Skip to content
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cam72cam.immersiverailroading.entity.*;
import cam72cam.immersiverailroading.entity.physics.chrono.ServerChronoState;
import cam72cam.immersiverailroading.gui.AugmentFilterGUI;
import cam72cam.immersiverailroading.gui.overlay.GuiBuilder;
import cam72cam.immersiverailroading.items.ItemPaintBrush;
import cam72cam.immersiverailroading.library.GuiTypes;
Expand Down Expand Up @@ -86,6 +87,7 @@ public void commonEvent(ModEvent event) {
Packet.register(ClientPartDragging.SeatPacket::new, PacketDirection.ClientToServer);
Packet.register(GuiBuilder.ControlChangePacket::new, PacketDirection.ClientToServer);
Packet.register(ItemPaintBrush.PaintBrushPacket::new, PacketDirection.ClientToServer);
Packet.register(AugmentFilterGUI.AugmentFilterChangePacket::new, PacketDirection.ClientToServer);

ServerChronoState.register();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ public double getDirectFrictionNewtons(List<Vec3i> track) {
for (Vec3i bp : track) {
TileRailBase te = getWorld().getBlockEntity(bp, TileRailBase.class);
if (te != null) {
if (te.getAugment() == Augment.SPEED_RETARDER) {
if (te.getAugment() == Augment.SPEED_RETARDER && te.canInteractWith(this)) {
double red = getWorld().getRedstone(bp);
retardedNewtons += red / 15f / track.size() * newtons;
}
Expand Down
187 changes: 187 additions & 0 deletions src/main/java/cam72cam/immersiverailroading/gui/AugmentFilterGUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
package cam72cam.immersiverailroading.gui;

import cam72cam.immersiverailroading.library.*;
import cam72cam.immersiverailroading.tile.TileRailBase;
import cam72cam.mod.entity.Player;
import cam72cam.mod.gui.helpers.GUIHelpers;
import cam72cam.mod.gui.screen.*;
import cam72cam.mod.math.Vec3i;
import cam72cam.mod.net.Packet;
import cam72cam.mod.render.opengl.RenderState;
import cam72cam.mod.serialization.TagField;
import cam72cam.mod.text.TextUtil;

import java.util.function.Function;

import static cam72cam.immersiverailroading.gui.ClickListHelper.next;

public class AugmentFilterGUI implements IScreen {
private final Vec3i pos;
private final Augment augment;
private final Augment.Properties properties;
private TextField includeTags;
private TextField excludeTags;
private Button stockDetectorMode;
private Button redstoneMode;
private CheckBox pushpull;
private Button couplerMode;
private Button locoControlMode;

public AugmentFilterGUI(TileRailBase tileRailBase) {
this.pos = tileRailBase.getPos();
this.augment = tileRailBase.getAugment();
this.properties = tileRailBase.getAugmentProperties() == null
? Augment.Properties.EMPTY
: tileRailBase.getAugmentProperties();
}

@Override
public void init(IScreenBuilder screen) {
int xtop = -GUIHelpers.getScreenWidth() / 2;
int ytop = -GUIHelpers.getScreenHeight() / 4;

int xOffset = 0;
int yOffset = 40;

int buttonWidth = 220;
int buttonHeight = 20;

includeTags = new TextField(screen, xtop + xOffset, ytop + yOffset, buttonWidth-1, buttonHeight);
includeTags.setText(properties.positiveFilter);
includeTags.setValidator(s -> {
properties.positiveFilter = s;
return true;
});
yOffset += 40;

excludeTags = new TextField(screen, xtop + xOffset, ytop + yOffset, buttonWidth-1, buttonHeight);
excludeTags.setText(properties.negativeFilter);
excludeTags.setValidator(s -> {
properties.negativeFilter = s;
return true;
});
yOffset += 25;

Function<Enum<?>, String> translate = e -> TextUtil.translate(e.toString());

stockDetectorMode = new Button(screen, xtop + xOffset, ytop + yOffset, buttonWidth, buttonHeight, GuiText.SELECTOR_AUGMENT_DETECT + translate.apply(properties.stockDetectorMode)) {
@Override
public void onClick(Player.Hand hand) {
properties.stockDetectorMode = next(properties.stockDetectorMode, Player.Hand.PRIMARY);
stockDetectorMode.setText(GuiText.SELECTOR_AUGMENT_DETECT + translate.apply(properties.stockDetectorMode));
}
};
stockDetectorMode.setEnabled(this.augment == Augment.DETECTOR);
yOffset += 25;

redstoneMode = new Button(screen, xtop + xOffset, ytop + yOffset, buttonWidth, buttonHeight, GuiText.SELECTOR_AUGMENT_REDSTONE + translate.apply(properties.redstoneMode)) {
@Override
public void onClick(Player.Hand hand) {
properties.redstoneMode = next(properties.redstoneMode, Player.Hand.PRIMARY);
redstoneMode.setText(GuiText.SELECTOR_AUGMENT_REDSTONE + translate.apply(properties.redstoneMode));
}
};
redstoneMode.setEnabled(this.augment == Augment.COUPLER
|| this.augment == Augment.ITEM_LOADER
|| this.augment == Augment.ITEM_UNLOADER
|| this.augment == Augment.FLUID_LOADER
|| this.augment == Augment.FLUID_UNLOADER);
yOffset += 25;

pushpull = new CheckBox(screen, xtop + xOffset, ytop + yOffset, GuiText.SELECTOR_AUGMENT_PUSHPULL.toString(), properties.pushpull) {
@Override
public void onClick(Player.Hand hand) {
properties.pushpull = !properties.pushpull;
pushpull.setChecked(properties.pushpull);
}
};
pushpull.setEnabled(this.augment == Augment.COUPLER
|| this.augment == Augment.ITEM_LOADER
|| this.augment == Augment.ITEM_UNLOADER
|| this.augment == Augment.FLUID_LOADER
|| this.augment == Augment.FLUID_UNLOADER);
yOffset += 15;

couplerMode = new Button(screen, xtop + xOffset, ytop + yOffset, buttonWidth, buttonHeight, GuiText.SELECTOR_AUGMENT_COUPLER + translate.apply(properties.couplerAugmentMode)) {
@Override
public void onClick(Player.Hand hand) {
properties.couplerAugmentMode = next(properties.couplerAugmentMode, Player.Hand.PRIMARY);
couplerMode.setText(GuiText.SELECTOR_AUGMENT_COUPLER + translate.apply(properties.couplerAugmentMode));
}
};
couplerMode.setEnabled(this.augment == Augment.COUPLER);
yOffset += 25;

locoControlMode = new Button(screen, xtop + xOffset, ytop + yOffset, buttonWidth, buttonHeight, GuiText.SELECTOR_AUGMENT_CONTROL + translate.apply(properties.locoControlMode)) {
@Override
public void onClick(Player.Hand hand) {
properties.locoControlMode = next(properties.locoControlMode, Player.Hand.PRIMARY);
locoControlMode.setText(GuiText.SELECTOR_AUGMENT_CONTROL + translate.apply(properties.locoControlMode));
}
};
locoControlMode.setEnabled(this.augment == Augment.LOCO_CONTROL);
}

@Override
public void onEnterKey(IScreenBuilder builder) {
builder.close();
}

@Override
public void onClose() {
if(properties.positiveFilter == null) {
properties.positiveFilter = "";
}
if (properties.negativeFilter == null) {
properties.negativeFilter = "";
}
new AugmentFilterChangePacket(pos, properties).sendToServer();
}

@Override
public void draw(IScreenBuilder builder, RenderState state) {
IScreen.super.draw(builder, state);

GUIHelpers.drawRect(0, 0, GUIHelpers.getScreenWidth(), GUIHelpers.getScreenHeight(), 0x88000000);

GUIHelpers.drawRect(0, 0, 220, GUIHelpers.getScreenHeight(), 0xCC000000);

int xtop = -GUIHelpers.getScreenWidth() / 2;
int ytop = -GUIHelpers.getScreenHeight() / 4;

int xOffset = 110;
int yOffset = 30;
GUIHelpers.drawCenteredString(GuiText.LABEL_CURRENT_AUGMENT + this.augment.toString(), xOffset, 10, 0xFFFFFFFF);

GUIHelpers.drawCenteredString(GuiText.LABEL_INCLUDED_TAG.toString(), xOffset, yOffset, 0xFFFFFFFF);
includeTags.setText(properties.positiveFilter);
yOffset+=40;

GUIHelpers.drawCenteredString(GuiText.LABEL_EXCLUDED_TAG.toString(), xOffset, yOffset, 0xFFFFFFFF);
excludeTags.setText(properties.negativeFilter);
}

public static class AugmentFilterChangePacket extends Packet {
@TagField
Vec3i pos;

@TagField
Augment.Properties properties;

public AugmentFilterChangePacket() {
}

public AugmentFilterChangePacket(Vec3i pos, Augment.Properties filter) {
this.pos = pos;
this.properties = filter;
}

@Override
protected void handle() {
TileRailBase railBase = this.getWorld().getBlockEntity(pos, TileRailBase.class);
if (railBase != null && railBase.getAugment() != null) {
railBase.setAugmentProperties(properties);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import cam72cam.mod.math.Vec3d;
import cam72cam.mod.math.Vec3i;
import cam72cam.mod.serialization.TagField;
import cam72cam.mod.text.TextUtil;
import cam72cam.mod.util.Facing;
import cam72cam.mod.world.World;

Expand Down Expand Up @@ -117,7 +116,7 @@ public List<String> getTooltip(ItemStack stack)

@Override
public String getCustomName(ItemStack stack) {
return TextUtil.translate("item.immersiverailroading:item_augment." + new Data(stack).augment.name() + ".name");
return new Data(stack).augment.toString();
}

public static class Data extends ItemDataSerializer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,31 +138,15 @@ public List<String> getTooltip(ItemStack stack)

@Override
public ClickResult onClickBlock(Player player, World world, Vec3i pos, Player.Hand hand, Facing facing, Vec3d hit) {
if (BlockUtil.isIRRail(world, pos)) {
TileRailBase te = world.getBlockEntity(pos, TileRailBase.class);
if (te.getAugment() != null) {
switch(te.getAugment()) {
case DETECTOR:
case LOCO_CONTROL:
case FLUID_LOADER:
case FLUID_UNLOADER:
case ITEM_LOADER:
case ITEM_UNLOADER:
if (world.isServer) {
Data data = new Data(player.getHeldItem(hand));
boolean set = te.setAugmentFilter(data.def != null ? data.def.defID : null);
if (set) {
player.sendMessage(ChatText.SET_AUGMENT_FILTER.getMessage(data.def != null ? data.def.name() : "Unknown"));
} else {
player.sendMessage(ChatText.RESET_AUGMENT_FILTER.getMessage());
}
}
return ClickResult.ACCEPTED;
default:
break;
}
}
}
// if(world.isServer && BlockUtil.isIRRail(world, pos)) {
// TileRailBase base = world.getBlockEntity(pos, TileRailBase.class);
// //TODO
// if (base.getAugment() != null) {
// Augment.Properties properties = base.getAugmentProperties();
//// properties.positiveFilter +=
// }
// }

return tryPlaceStock(player, world, pos, hand, null);
}

Expand Down
82 changes: 82 additions & 0 deletions src/main/java/cam72cam/immersiverailroading/library/Augment.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package cam72cam.immersiverailroading.library;

import cam72cam.mod.render.Color;
import cam72cam.mod.serialization.SerializationException;
import cam72cam.mod.serialization.TagCompound;
import cam72cam.mod.serialization.TagField;
import cam72cam.mod.serialization.TagMapped;
import cam72cam.mod.text.TextUtil;

public enum Augment {
SPEED_RETARDER,
Expand Down Expand Up @@ -40,4 +45,81 @@ public Color color() {
}
return Color.WHITE;
}

@Override
public String toString() {
return TextUtil.translate("item.immersiverailroading:item_augment." + this.name() + ".name");
}

@TagMapped(PropertyMapper.class)
public static class Properties {
public static final Properties EMPTY = new Properties("", "",
CouplerAugmentMode.ENGAGED,
LocoControlMode.THROTTLE,
RedstoneMode.ENABLED,
true,
StockDetectorMode.SIMPLE);

public String positiveFilter;
public String negativeFilter;
public CouplerAugmentMode couplerAugmentMode;
public LocoControlMode locoControlMode;
public RedstoneMode redstoneMode;
public boolean pushpull;
public StockDetectorMode stockDetectorMode;

public Properties() {
}

public Properties(String positiveFilter, String negativeFilter, CouplerAugmentMode couplerAugmentMode,
LocoControlMode locoControlMode, RedstoneMode redstoneMode, boolean pushpull, StockDetectorMode stockDetectorMode) {
this.positiveFilter = positiveFilter;
this.negativeFilter = negativeFilter;
this.couplerAugmentMode = couplerAugmentMode;
this.locoControlMode = locoControlMode;
this.redstoneMode = redstoneMode;
this.pushpull = pushpull;
this.stockDetectorMode = stockDetectorMode;
}

public TagCompound toNBT() {
TagCompound compound = new TagCompound();
compound.setString("positive", positiveFilter);
compound.setString("negative", negativeFilter);
compound.setString("coupler", couplerAugmentMode.name());
compound.setString("loco", locoControlMode.name());
compound.setString("redstone", redstoneMode.name());
compound.setBoolean("pushpull", pushpull);
compound.setString("detector", stockDetectorMode.name());
return compound;
}

public static Properties fromNBT(TagCompound compound) {
Properties properties = new Properties(
compound.getString("positive"),
compound.getString("negative"),
CouplerAugmentMode.valueOf(compound.getString("coupler")),
LocoControlMode.valueOf(compound.getString("loco")),
RedstoneMode.valueOf(compound.getString("redstone")),
compound.getBoolean("pushpull"),
StockDetectorMode.valueOf(compound.getString("detector")));
return properties;
}
}

public static class PropertyMapper implements cam72cam.mod.serialization.TagMapper<Properties> {
@Override
public TagAccessor<Properties> apply(Class<Properties> type, String fieldName, TagField tag) throws SerializationException {
return new TagAccessor<>(
(t, p) -> {
if (p == null) {
t.remove(fieldName);
} else {
t.set(fieldName, p.toNBT());
}
},
t -> t.hasKey(fieldName) ? Properties.fromNBT(t.get(fieldName)) : Properties.EMPTY
);
}
}
}
Loading