mirror of
https://github.com/Anuken/Mindustry.git
synced 2026-04-27 07:50:54 -07:00
Better API for saving block configs
This commit is contained in:
parent
8eeb668945
commit
5c00ced1e3
18 changed files with 61 additions and 87 deletions
|
|
@ -294,6 +294,7 @@ public class EntityProcess extends BaseProcessor{
|
|||
//build method using same params/returns
|
||||
MethodSpec.Builder mbuilder = MethodSpec.methodBuilder(first.name()).addModifiers(first.is(Modifier.PRIVATE) ? Modifier.PRIVATE : Modifier.PUBLIC);
|
||||
if(isFinal || entry.value.contains(s -> s.has(Final.class))) mbuilder.addModifiers(Modifier.FINAL);
|
||||
if(entry.value.contains(s -> s.has(CallSuper.class))) mbuilder.addAnnotation(CallSuper.class); //add callSuper here if necessary
|
||||
if(first.is(Modifier.STATIC)) mbuilder.addModifiers(Modifier.STATIC);
|
||||
mbuilder.addTypeVariables(first.typeVariables().map(TypeVariableName::get));
|
||||
mbuilder.returns(first.retn());
|
||||
|
|
|
|||
|
|
@ -121,7 +121,7 @@ public class PhysicsProcess implements AsyncProcess{
|
|||
ref.position.set(entity);
|
||||
|
||||
//add delta velocity - this doesn't work very well yet
|
||||
entity.vel().add(ref.velocity).sub(ref.lastVelocity);
|
||||
//entity.vel().add(ref.velocity).sub(ref.lastVelocity);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -144,6 +144,18 @@ abstract class TileComp implements Posc, Teamc, Healthc, Tilec, Timerc, QuadTree
|
|||
//endregion
|
||||
//region utility methods
|
||||
|
||||
/** Configure with the current, local player. */
|
||||
public void configure(Object value){
|
||||
//save last used config
|
||||
block.lastConfig = value;
|
||||
Call.onTileConfig(player, this, value);
|
||||
}
|
||||
|
||||
/** Configure from a server. */
|
||||
public void configureAny(Object value){
|
||||
Call.onTileConfig(null, this, value);
|
||||
}
|
||||
|
||||
public void applyBoost(float intensity, float duration){
|
||||
timeScale = Math.max(timeScale, intensity);
|
||||
timeScaleDuration = Math.max(timeScaleDuration, duration);
|
||||
|
|
@ -670,7 +682,9 @@ abstract class TileComp implements Posc, Teamc, Healthc, Tilec, Timerc, QuadTree
|
|||
/** Called after the block is placed by this client. */
|
||||
@CallSuper
|
||||
public void playerPlaced(){
|
||||
|
||||
if(block.saveConfig && block.lastConfig != null){
|
||||
configure(block.lastConfig);
|
||||
}
|
||||
}
|
||||
|
||||
/** Called after the block is placed by anyone. */
|
||||
|
|
@ -692,7 +706,7 @@ abstract class TileComp implements Posc, Teamc, Healthc, Tilec, Timerc, QuadTree
|
|||
if(!tempTiles.isEmpty()){
|
||||
Tile toLink = tempTiles.first();
|
||||
if(!toLink.entity.power().links.contains(pos())){
|
||||
toLink.configureAny(pos());
|
||||
toLink.entity.configureAny(pos());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -287,7 +287,9 @@ public class Schematics implements Loadable{
|
|||
tile.rotation(st.rotation);
|
||||
|
||||
Object config = st.config;
|
||||
tile.configureAny(config);
|
||||
if(tile.entity != null){
|
||||
tile.entity.configureAny(config);
|
||||
}
|
||||
|
||||
if(st.block instanceof Drill){
|
||||
tile.getLinkedTiles(t -> t.setOverlay(Blocks.oreCopper));
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ import arc.struct.Array;
|
|||
import arc.struct.EnumSet;
|
||||
import arc.struct.*;
|
||||
import arc.util.*;
|
||||
import arc.util.ArcAnnotate.*;
|
||||
import arc.util.pooling.*;
|
||||
import mindustry.annotations.Annotations.*;
|
||||
import mindustry.ctype.*;
|
||||
|
|
@ -55,6 +56,10 @@ public class Block extends UnlockableContent{
|
|||
public final BlockBars bars = new BlockBars();
|
||||
public final Consumers consumes = new Consumers();
|
||||
|
||||
/** the last configuration value applied to this block. */
|
||||
public @Nullable Object lastConfig;
|
||||
/** whether to save the last config and apply it to newly placed blocks */
|
||||
public boolean saveConfig = false;
|
||||
/** whether this block has a tile entity that updates */
|
||||
public boolean update;
|
||||
/** whether this block has health and can be destroyed */
|
||||
|
|
|
|||
|
|
@ -95,15 +95,6 @@ public class Tile implements Position, QuadTreeObject{
|
|||
return -1;
|
||||
}
|
||||
|
||||
/** Configure a tile with the current, local player. */
|
||||
public void configure(Object value){
|
||||
if(entity != null) Call.onTileConfig(player, entity, value);
|
||||
}
|
||||
|
||||
public void configureAny(Object value){
|
||||
if(entity != null) Call.onTileConfig(null, entity, value);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends TileEntity> T ent(){
|
||||
return (T)entity;
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ public class Door extends Wall{
|
|||
return;
|
||||
}
|
||||
|
||||
tile.configure(!open);
|
||||
configure(!open);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -20,15 +20,15 @@ import mindustry.world.meta.*;
|
|||
import static mindustry.Vars.*;
|
||||
|
||||
public class ItemBridge extends Block{
|
||||
private static BuildRequest otherReq;
|
||||
|
||||
public final int timerTransport = timers++;
|
||||
public int range;
|
||||
public float transportTime = 2f;
|
||||
public @Load("@-end") TextureRegion endRegion;
|
||||
public @Load("@-bridge") TextureRegion bridgeRegion;
|
||||
public @Load("@-arrow") TextureRegion arrowRegion;
|
||||
|
||||
private static BuildRequest otherReq;
|
||||
private static int lastPlaced = -1;
|
||||
public int lastPlaced = -1;
|
||||
|
||||
public ItemBridge(String name){
|
||||
super(name);
|
||||
|
|
@ -132,7 +132,7 @@ public class ItemBridge extends Block{
|
|||
public void playerPlaced(){
|
||||
Tile link = findLink(tile.x, tile.y);
|
||||
if(linkValid(tile, link)){
|
||||
link.configure(tile.pos());
|
||||
configure(tile.pos());
|
||||
}
|
||||
|
||||
lastPlaced = tile.pos();
|
||||
|
|
@ -165,9 +165,9 @@ public class ItemBridge extends Block{
|
|||
public boolean onConfigureTileTapped(Tilec other){
|
||||
if(linkValid(tile, other.tile())){
|
||||
if(link == other.pos()){
|
||||
tile.configure(-1);
|
||||
configure(-1);
|
||||
}else{
|
||||
tile.configure(other.pos());
|
||||
configure(other.pos());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -213,15 +213,15 @@ public class MassDriver extends Block{
|
|||
@Override
|
||||
public boolean onConfigureTileTapped(Tilec other){
|
||||
if(this == other){
|
||||
tile.configure(-1);
|
||||
configure(-1);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(link == other.pos()){
|
||||
tile.configure(-1);
|
||||
configure(-1);
|
||||
return false;
|
||||
}else if(other.block() instanceof MassDriver && other.dst(tile) <= range && other.team() == team){
|
||||
tile.configure(other.pos());
|
||||
configure(other.pos());
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@ import mindustry.world.meta.*;
|
|||
import static mindustry.Vars.*;
|
||||
|
||||
public class Sorter extends Block{
|
||||
private static Item lastItem;
|
||||
public boolean invert;
|
||||
|
||||
public Sorter(String name){
|
||||
|
|
@ -27,6 +26,8 @@ public class Sorter extends Block{
|
|||
group = BlockGroup.transportation;
|
||||
configurable = true;
|
||||
unloadable = false;
|
||||
saveConfig = true;
|
||||
|
||||
config(Item.class, (tile, item) -> ((SorterEntity)tile).sortItem = item);
|
||||
configClear(tile -> ((SorterEntity)tile).sortItem = null);
|
||||
}
|
||||
|
|
@ -49,13 +50,6 @@ public class Sorter extends Block{
|
|||
public class SorterEntity extends TileEntity{
|
||||
@Nullable Item sortItem;
|
||||
|
||||
@Override
|
||||
public void playerPlaced(){
|
||||
if(lastItem != null){
|
||||
tile.configure(lastItem);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configured(Playerc player, Object value){
|
||||
super.configured(player, value);
|
||||
|
|
@ -140,14 +134,14 @@ public class Sorter extends Block{
|
|||
|
||||
@Override
|
||||
public void buildConfiguration(Table table){
|
||||
ItemSelection.buildTable(table, content.items(), () -> sortItem, item -> tile.configure(lastItem = item));
|
||||
ItemSelection.buildTable(table, content.items(), () -> sortItem, item -> configure(item));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onConfigureTileTapped(Tilec other){
|
||||
if(this == other){
|
||||
control.input.frag.config.hideConfig();
|
||||
tile.configure(lastItem = null);
|
||||
configure(null);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -95,7 +95,7 @@ public class MessageBlock extends Block{
|
|||
text = message;
|
||||
multiline = true;
|
||||
maxLength = maxTextLength;
|
||||
accepted = tile::configure;
|
||||
accepted = str -> configure(str);
|
||||
}});
|
||||
}else{
|
||||
FloatingDialog dialog = new FloatingDialog("$editmessage");
|
||||
|
|
@ -115,7 +115,7 @@ public class MessageBlock extends Block{
|
|||
});
|
||||
a.setMaxLength(maxTextLength);
|
||||
dialog.buttons.button("$ok", () -> {
|
||||
tile.configure(a.getText());
|
||||
configure(a.getText());
|
||||
dialog.hide();
|
||||
}).size(130f, 60f);
|
||||
dialog.update(() -> {
|
||||
|
|
|
|||
|
|
@ -13,8 +13,6 @@ import mindustry.world.*;
|
|||
import static mindustry.Vars.*;
|
||||
|
||||
public class LightBlock extends Block{
|
||||
private static int lastColor = 0;
|
||||
|
||||
public float brightness = 0.9f;
|
||||
public float radius = 200f;
|
||||
public @Load("@-top") TextureRegion topRegion;
|
||||
|
|
@ -24,19 +22,14 @@ public class LightBlock extends Block{
|
|||
hasPower = true;
|
||||
update = true;
|
||||
configurable = true;
|
||||
saveConfig = true;
|
||||
|
||||
config(Integer.class, (tile, value) -> ((LightEntity)tile).color = value);
|
||||
}
|
||||
|
||||
public class LightEntity extends TileEntity{
|
||||
public int color = Pal.accent.rgba();
|
||||
|
||||
@Override
|
||||
public void playerPlaced(){
|
||||
if(lastColor != 0){
|
||||
tile.configure(lastColor);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(){
|
||||
super.draw();
|
||||
|
|
@ -50,10 +43,7 @@ public class LightBlock extends Block{
|
|||
@Override
|
||||
public void buildConfiguration(Table table){
|
||||
table.button(Icon.pencil, () -> {
|
||||
ui.picker.show(Tmp.c1.set(color).a(0.5f), false, res -> {
|
||||
color = res.rgba();
|
||||
lastColor = color;
|
||||
});
|
||||
ui.picker.show(Tmp.c1.set(color).a(0.5f), false, res -> configure(res.rgba()));
|
||||
control.input.frag.config.hideConfig();
|
||||
}).size(40f);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -285,7 +285,7 @@ public class PowerNode extends PowerBlock{
|
|||
});
|
||||
tempTileEnts.each(valid, other -> {
|
||||
if(!power.links.contains(other.pos())){
|
||||
tile.configureAny(other.pos());
|
||||
configureAny(other.pos());
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -300,7 +300,7 @@ public class PowerNode extends PowerBlock{
|
|||
@Override
|
||||
public boolean onConfigureTileTapped(Tilec other){
|
||||
if(linkValid(this, other)){
|
||||
tile.configure(other.pos());
|
||||
configure(other.pos());
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -309,12 +309,12 @@ public class PowerNode extends PowerBlock{
|
|||
int[] total = {0};
|
||||
getPotentialLinks(tile, link -> {
|
||||
if(!insulated(this, link) && total[0]++ < maxNodes){
|
||||
tile.configure(link.pos());
|
||||
configure(link.pos());
|
||||
}
|
||||
});
|
||||
}else{
|
||||
while(power.links.size > 0){
|
||||
tile.configure(power.links.get(0));
|
||||
configure(power.links.get(0));
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
package mindustry.world.blocks.sandbox;
|
||||
|
||||
import arc.*;
|
||||
import arc.graphics.g2d.*;
|
||||
import arc.scene.ui.layout.*;
|
||||
import arc.util.*;
|
||||
|
|
@ -15,7 +14,6 @@ import mindustry.world.meta.*;
|
|||
import static mindustry.Vars.*;
|
||||
|
||||
public class ItemSource extends Block{
|
||||
private static Item lastItem;
|
||||
|
||||
public ItemSource(String name){
|
||||
super(name);
|
||||
|
|
@ -24,6 +22,8 @@ public class ItemSource extends Block{
|
|||
solid = true;
|
||||
group = BlockGroup.transportation;
|
||||
configurable = true;
|
||||
saveConfig = true;
|
||||
|
||||
config(Item.class, (tile, item) -> ((ItemSourceEntity)tile).outputItem = item);
|
||||
configClear(tile -> ((ItemSourceEntity)tile).outputItem = null);
|
||||
}
|
||||
|
|
@ -47,13 +47,6 @@ public class ItemSource extends Block{
|
|||
public class ItemSourceEntity extends TileEntity{
|
||||
Item outputItem;
|
||||
|
||||
@Override
|
||||
public void playerPlaced(){
|
||||
if(lastItem != null){
|
||||
Core.app.post(() -> tile.configure(lastItem));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(){
|
||||
super.draw();
|
||||
|
|
@ -76,14 +69,14 @@ public class ItemSource extends Block{
|
|||
|
||||
@Override
|
||||
public void buildConfiguration(Table table){
|
||||
ItemSelection.buildTable(table, content.items(), () -> outputItem, item -> tile.configure(lastItem = item));
|
||||
ItemSelection.buildTable(table, content.items(), () -> outputItem, item -> configure(item));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onConfigureTileTapped(Tilec other){
|
||||
if(this == other){
|
||||
control.input.frag.config.hideConfig();
|
||||
tile.configure(lastItem = null);
|
||||
configure(null);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
package mindustry.world.blocks.sandbox;
|
||||
|
||||
import arc.*;
|
||||
import arc.graphics.g2d.*;
|
||||
import arc.scene.ui.layout.*;
|
||||
import arc.util.ArcAnnotate.*;
|
||||
|
|
@ -16,7 +15,6 @@ import mindustry.world.blocks.*;
|
|||
import static mindustry.Vars.*;
|
||||
|
||||
public class LiquidSource extends Block{
|
||||
public static Liquid lastLiquid;
|
||||
|
||||
public LiquidSource(String name){
|
||||
super(name);
|
||||
|
|
@ -68,27 +66,20 @@ public class LiquidSource extends Block{
|
|||
|
||||
@Override
|
||||
public void buildConfiguration(Table table){
|
||||
ItemSelection.buildTable(table, content.liquids(), () -> source, liquid -> tile.configure(lastLiquid = liquid));
|
||||
ItemSelection.buildTable(table, content.liquids(), () -> source, liquid -> configure(liquid));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onConfigureTileTapped(Tilec other){
|
||||
if(this == other){
|
||||
control.input.frag.config.hideConfig();
|
||||
tile.configure(lastLiquid = null);
|
||||
configure(null);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playerPlaced(){
|
||||
if(lastLiquid != null){
|
||||
Core.app.post(() -> tile.configure(lastLiquid));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Liquid config(){
|
||||
return source;
|
||||
|
|
|
|||
|
|
@ -17,8 +17,6 @@ public class Unloader extends Block{
|
|||
public float speed = 1f;
|
||||
public final int timerUnload = timers++;
|
||||
|
||||
private static Item lastItem;
|
||||
|
||||
public Unloader(String name){
|
||||
super(name);
|
||||
update = true;
|
||||
|
|
@ -26,6 +24,8 @@ public class Unloader extends Block{
|
|||
health = 70;
|
||||
hasItems = true;
|
||||
configurable = true;
|
||||
saveConfig = true;
|
||||
|
||||
config(Item.class, (tile, item) -> {
|
||||
tile.items().clear();
|
||||
((UnloaderEntity)tile).sortItem = item;
|
||||
|
|
@ -69,13 +69,6 @@ public class Unloader extends Block{
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playerPlaced(){
|
||||
if(lastItem != null){
|
||||
tile.configure(lastItem);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateTile(){
|
||||
if(timer(timerUnload, speed / timeScale()) && items.total() == 0){
|
||||
|
|
@ -101,14 +94,14 @@ public class Unloader extends Block{
|
|||
|
||||
@Override
|
||||
public void buildConfiguration(Table table){
|
||||
ItemSelection.buildTable(table, content.items(), () -> tile.<UnloaderEntity>ent().sortItem, item -> tile.configure(lastItem = item));
|
||||
ItemSelection.buildTable(table, content.items(), () -> tile.<UnloaderEntity>ent().sortItem, item -> configure(item));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onConfigureTileTapped(Tilec other){
|
||||
if(this == other){
|
||||
control.input.frag.config.hideConfig();
|
||||
tile.configure(lastItem = null);
|
||||
configure(null);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -84,7 +84,7 @@ public class CommandCenter extends Block{
|
|||
Table buttons = new Table();
|
||||
|
||||
for(UnitCommand cmd : UnitCommand.all){
|
||||
buttons.button(commandRegions[cmd.ordinal()], Styles.clearToggleTransi, () -> tile.configure(cmd.ordinal()))
|
||||
buttons.button(commandRegions[cmd.ordinal()], Styles.clearToggleTransi, () -> configure(cmd.ordinal()))
|
||||
.size(44).group(group).update(b -> b.setChecked(command == cmd));
|
||||
}
|
||||
table.add(buttons);
|
||||
|
|
|
|||
|
|
@ -147,7 +147,7 @@ public class UnitFactory extends Block{
|
|||
public void buildConfiguration(Table table){
|
||||
Array<UnitType> units = Array.with(plans).map(u -> u.unit);
|
||||
|
||||
ItemSelection.buildTable(table, units, () -> currentPlan == -1 ? null : plans[currentPlan].unit, unit -> tile.configure(units.indexOf(unit)));
|
||||
ItemSelection.buildTable(table, units, () -> currentPlan == -1 ? null : plans[currentPlan].unit, unit -> configure(units.indexOf(unit)));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue