Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiblock and Cargo cars support #1497

Open
wants to merge 43 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
12edeed
Cargo test
Goldenfield192 Jun 14, 2024
321f1a1
Cargo test
Goldenfield192 Jun 14, 2024
9d092eb
Simplify the code
Goldenfield192 Jun 14, 2024
3953e55
Add output
Goldenfield192 Jun 14, 2024
17ec49c
Minor fix
Goldenfield192 Jun 15, 2024
c77d981
Documents
Goldenfield192 Jun 15, 2024
4411247
Fixes: cargoFillFront&cargoFillRear
Goldenfield192 Jun 15, 2024
6f990f0
Fixes: Some code should be server-only
Goldenfield192 Jun 15, 2024
740ab4a
Initial commit of multiblocks
Goldenfield192 Jun 18, 2024
6082974
update
Goldenfield192 Jun 19, 2024
717bc84
Merge remote-tracking branch 'origin/master'
Goldenfield192 Jun 19, 2024
91bdadd
update
Goldenfield192 Jun 20, 2024
202cad8
update
Goldenfield192 Jun 20, 2024
8a801c2
update
Goldenfield192 Jun 21, 2024
6627bc7
update
Goldenfield192 Jun 21, 2024
2087403
fix
Goldenfield192 Jun 22, 2024
c90e49d
A hack to render background
Goldenfield192 Jun 22, 2024
6a8a1a2
Fixes for transporters
Goldenfield192 Jun 22, 2024
455449a
Update: tank stock list for transporters
Goldenfield192 Jun 23, 2024
c7d6d0a
Update: Fluid unloader
Goldenfield192 Jun 26, 2024
25fcc94
Pull candidate #1
Goldenfield192 Jun 29, 2024
bdf4609
Bug fixes
Goldenfield192 Jun 29, 2024
ae3474c
Merge pull request #2 from Goldenfield192/cargo_test
Goldenfield192 Jun 30, 2024
5948984
Fixes: handle merges from cargo_fill
Goldenfield192 Jun 30, 2024
b4d665d
Cleaning
Goldenfield192 Jun 30, 2024
e44676c
Cleaning
Goldenfield192 Jun 30, 2024
717adbc
Fix: bugs
Goldenfield192 Jul 1, 2024
82af704
Fix: Fatal bug with Cargo fill(ConcurrentModificationException)
Goldenfield192 Jul 6, 2024
749fb28
Feat: Readout-like
Goldenfield192 Jul 19, 2024
daae41a
backup
Goldenfield192 Jul 23, 2024
d34d705
Feat: Abstraction of control groups
Goldenfield192 Jul 23, 2024
83a1674
Feat: Abstraction of control groups part 2
Goldenfield192 Jul 23, 2024
289e900
Merge branch 'multiblock' into animation_abstraction
Goldenfield192 Jul 23, 2024
23aaa1e
Merge pull request #4 from Goldenfield192/animation_abstraction
Goldenfield192 Jul 23, 2024
fbca6c6
Feat: Make multiblock render system be based on IAnimatable
Goldenfield192 Jul 23, 2024
888a8f2
Feat: Multiblock animations and new GUI
Goldenfield192 Jul 25, 2024
e7c64b3
Fix: NPE caused by CustomTransporterMultiblock$refreshTrackPositions
Goldenfield192 Jul 26, 2024
d65a09d
Feat: configurable stock interaction bound
Goldenfield192 Jul 26, 2024
3e6a2bb
Feat: Cargo support and text changes
Goldenfield192 Oct 24, 2024
6fdc79a
Comment changes
Goldenfield192 Oct 24, 2024
18b7b7e
Feat: Separate multiblock functions from manual and other optimizations
Goldenfield192 Oct 26, 2024
204a571
Fix: Gui issues
Goldenfield192 Oct 26, 2024
c33621f
Code cleanup
Goldenfield192 Oct 29, 2024
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
1 change: 1 addition & 0 deletions src/main/java/cam72cam/immersiverailroading/IRItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class IRItems {
public static ItemRailAugment ITEM_AUGMENT = new ItemRailAugment();
public static ItemTrackBlueprint ITEM_TRACK_BLUEPRINT = new ItemTrackBlueprint();
public static ItemManual ITEM_MANUAL = new ItemManual();
public static ItemMultiblockBlueprint ITEM_MULTIBLOCK_BLUEPRINT = new ItemMultiblockBlueprint();
public static ItemRail ITEM_RAIL = new ItemRail();
public static ItemPlate ITEM_PLATE = new ItemPlate();
public static ItemCastRail ITEM_CAST_RAIL = new ItemCastRail();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ public void commonEvent(ModEvent event) {

Packet.register(BuildableStockSyncPacket::new, PacketDirection.ServerToClient);
Packet.register(ItemRailUpdatePacket::new, PacketDirection.ClientToServer);
Packet.register(ItemMultiblockUpdatePacket::new, PacketDirection.ClientToServer);
Packet.register(MRSSyncPacket::new, PacketDirection.ServerToClient);
Packet.register(MultiblockSelectCraftPacket::new, PacketDirection.ClientToServer);
Packet.register(MultiblockSetStockPacket::new, PacketDirection.ClientToServer);
Packet.register(MultiblockControlChangePacket::new, PacketDirection.ClientToServer);
Packet.register(PreviewRenderPacket::new, PacketDirection.ServerToClient);
Packet.register(SoundPacket::new, PacketDirection.ServerToClient);
Packet.register(KeyPressPacket::new, PacketDirection.ClientToServer);
Expand Down Expand Up @@ -113,6 +116,11 @@ public void commonEvent(ModEvent event) {
MultiblockRegistry.register(RailRollerMultiblock.NAME, new RailRollerMultiblock());
MultiblockRegistry.register(BoilerRollerMultiblock.NAME, new BoilerRollerMultiblock());
MultiblockRegistry.register(CastingMultiblock.NAME, new CastingMultiblock());
for (String s : DefinitionManager.multiblocks.keySet()) {
MultiblockRegistry.register(DefinitionManager.multiblocks.get(s).name,
new CustomTransporterMultiblock(DefinitionManager.multiblocks.get(s)));
}
TileMultiblockRender.registerOthers();
IRFuzzy.applyFallbacks();
break;
}
Expand Down Expand Up @@ -143,6 +151,7 @@ public void clientEvent(ModEvent event) {
ItemRender.register(IRItems.ITEM_PAINT_BRUSH, ObjItemRender.getModelFor(new Identifier(MODID, "models/item/paint_brush.obj"), new Vec3d(0.5, 0.25, 0.5), 3));
ItemRender.register(IRItems.ITEM_RADIO_CONTROL_CARD, new Identifier(MODID, "items/radio_card"));
ItemRender.register(IRItems.ITEM_MANUAL, new Identifier(MODID, "items/engineerslexicon"));
ItemRender.register(IRItems.ITEM_MULTIBLOCK_BLUEPRINT, new Identifier(MODID, "items/engineerslexicon"));
ItemRender.register(IRItems.ITEM_TRACK_EXCHANGER, new TrackExchangerModel());

IEntityRender<EntityMoveableRollingStock> stockRender = new IEntityRender<EntityMoveableRollingStock>() {
Expand Down Expand Up @@ -194,7 +203,7 @@ public void postRender(EntityMoveableRollingStock entity, RenderState state, flo
break;
case SETUP:
GlobalRender.registerItemMouseover(IRItems.ITEM_TRACK_BLUEPRINT, TrackBlueprintItemModel::renderMouseover);
GlobalRender.registerItemMouseover(IRItems.ITEM_MANUAL, MBBlueprintRender::renderMouseover);
GlobalRender.registerItemMouseover(IRItems.ITEM_MULTIBLOCK_BLUEPRINT, MBBlueprintRender::renderMouseover);

GlobalRender.registerOverlay((state, pt) -> {
Entity riding = MinecraftClient.getPlayer().getRiding();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public void onDragRelease(Control<?> control) {
}

@Override
protected float defaultControlPosition(Control<?> control) {
public float defaultControlPosition(Control<?> control) {
switch (control.part.type) {
case INDEPENDENT_BRAKE_X:
return getDefinition().isLinearBrakeControl() ? 0 : 0.5f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import cam72cam.immersiverailroading.ImmersiveRailroading;
import cam72cam.immersiverailroading.items.ItemPaintBrush;
import cam72cam.immersiverailroading.library.*;
import cam72cam.immersiverailroading.model.animation.ControlPositionMapper;
import cam72cam.immersiverailroading.model.animation.IAnimatable;
import cam72cam.immersiverailroading.model.part.Control;
import cam72cam.immersiverailroading.registry.DefinitionManager;
import cam72cam.immersiverailroading.registry.EntityRollingStockDefinition;
Expand All @@ -27,10 +29,9 @@

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

public class EntityRollingStock extends CustomEntity implements ITickable, IClickable, IKillable {
public class EntityRollingStock extends CustomEntity implements ITickable, IClickable, IKillable, IAnimatable {
@TagField("defID")
protected String defID;
@TagField("gauge")
Expand Down Expand Up @@ -265,6 +266,7 @@ public String getTexture() {
return texture;
}

@Override
public Matrix4 getModelMatrix() {
return this.modelMatrix.get(getPosition()).copy();
}
Expand All @@ -277,83 +279,77 @@ public boolean hasElectricalPower() {
@TagField(value="controlPositions", mapper = ControlPositionMapper.class)
protected Map<String, Pair<Boolean, Float>> controlPositions = new HashMap<>();

public void onDragStart(Control<?> control) {
setControlPressed(control, true);
}

public void onDrag(Control<?> control, double newValue) {
setControlPressed(control, true);
setControlPosition(control, (float)newValue);
}
@Override
public float defaultControlPosition(Control<?> control) {
return 0;
}

public void onDragRelease(Control<?> control) {
setControlPressed(control, false);
@Override
public Pair<Boolean, Float> getControlData(String control) {
return controlPositions.getOrDefault(control, Pair.of(false, 0f));
}

if (control.toggle) {
setControlPosition(control, Math.abs(getControlPosition(control) - 1));
}
if (control.press) {
setControlPosition(control, 0);
}
}
@Override
public Pair<Boolean, Float> getControlData(Control<?> control) {
return controlPositions.getOrDefault(control.controlGroup, Pair.of(false, defaultControlPosition(control)));
}

protected float defaultControlPosition(Control<?> control) {
return 0;
}
@Override
public float getControlPosition(Control<?> control) {
return getControlData(control).getRight();
}

public Pair<Boolean, Float> getControlData(String control) {
return controlPositions.getOrDefault(control, Pair.of(false, 0f));
}
@Override
public float getControlPosition(String control) {
return getControlData(control).getRight();
}

public Pair<Boolean, Float> getControlData(Control<?> control) {
return controlPositions.getOrDefault(control.controlGroup, Pair.of(false, defaultControlPosition(control)));
}
@Override
public void setControlPosition(Control<?> control, float val) {
val = Math.min(1, Math.max(0, val));
controlPositions.put(control.controlGroup, Pair.of(getControlPressed(control), val));
}

public boolean getControlPressed(Control<?> control) {
return getControlData(control).getLeft();
}
@Override
public void setControlPosition(String control, float val) {
val = Math.min(1, Math.max(0, val));
controlPositions.put(control, Pair.of(false, val));
}

public void setControlPressed(Control<?> control, boolean pressed) {
controlPositions.put(control.controlGroup, Pair.of(pressed, getControlPosition(control)));
}
@Override
public void setControlPositions(ModelComponentType type, float val) {
getDefinition().getModel().getControls().stream().filter(x -> x.part.type == type).forEach(c -> setControlPosition(c, val));
}

public float getControlPosition(Control<?> control) {
return getControlData(control).getRight();
public void onDragStart(Control<?> control) {
setControlPressed(control, true);
}

public float getControlPosition(String control) {
return getControlData(control).getRight();
public void onDrag(Control<?> control, double newValue) {
setControlPressed(control, true);
setControlPosition(control, (float)newValue);
}

public void setControlPosition(Control<?> control, float val) {
val = Math.min(1, Math.max(0, val));
controlPositions.put(control.controlGroup, Pair.of(getControlPressed(control), val));
}
public void onDragRelease(Control<?> control) {
setControlPressed(control, false);

if (control.toggle) {
setControlPosition(control, Math.abs(getControlPosition(control) - 1));
}
if (control.press) {
setControlPosition(control, 0);
}
}

public void setControlPosition(String control, float val) {
val = Math.min(1, Math.max(0, val));
controlPositions.put(control, Pair.of(false, val));
}
public boolean getControlPressed(Control<?> control) {
return getControlData(control).getLeft();
}

public void setControlPositions(ModelComponentType type, float val) {
getDefinition().getModel().getControls().stream().filter(x -> x.part.type == type).forEach(c -> setControlPosition(c, val));
}
public void setControlPressed(Control<?> control, boolean pressed) {
controlPositions.put(control.controlGroup, Pair.of(pressed, getControlPosition(control)));
}

public boolean playerCanDrag(Player player, Control<?> control) {
return control.part.type != ModelComponentType.INDEPENDENT_BRAKE_X || player.hasPermission(Permissions.BRAKE_CONTROL);
}


private static class ControlPositionMapper implements TagMapper<Map<String, Pair<Boolean, Float>>> {
@Override
public TagAccessor<Map<String, Pair<Boolean, Float>>> apply(
Class<Map<String, Pair<Boolean, Float>>> type,
String fieldName,
TagField tag) throws SerializationException {
return new TagAccessor<>(
(d, o) -> d.setMap(fieldName, o, Function.identity(), x -> new TagCompound().setBoolean("pressed", x.getLeft()).setFloat("pos", x.getRight())),
d -> d.getMap(fieldName, Function.identity(), x -> Pair.of(x.hasKey("pressed") && x.getBoolean("pressed"), x.getFloat("pos")))
);
}
}
}
64 changes: 57 additions & 7 deletions src/main/java/cam72cam/immersiverailroading/entity/Freight.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
import cam72cam.immersiverailroading.inventory.FilteredStackHandler;
import cam72cam.immersiverailroading.library.GuiTypes;
import cam72cam.immersiverailroading.library.Permissions;
import cam72cam.immersiverailroading.model.FreightModel;
import cam72cam.immersiverailroading.model.part.CargoUnload;
import cam72cam.immersiverailroading.registry.FreightDefinition;
import cam72cam.mod.entity.Entity;
import cam72cam.mod.entity.Living;
import cam72cam.mod.entity.Player;
import cam72cam.mod.entity.boundingbox.IBoundingBox;
import cam72cam.mod.entity.sync.TagSync;
import cam72cam.mod.item.ClickResult;
import cam72cam.mod.item.Fuzzy;
import cam72cam.mod.item.ItemStack;
import cam72cam.mod.math.Vec3d;
import cam72cam.mod.serialization.TagField;

import java.util.List;
Expand All @@ -28,6 +32,8 @@ public abstract class Freight extends EntityCoupleableRollingStock {
@TagField("PERCENT_FULL")
private int percentFull = 0;

protected transient int ticks = 0;

public abstract int getInventorySize();
public abstract int getInventoryWidth();

Expand All @@ -37,10 +43,10 @@ public FreightDefinition getDefinition() {
}

/*
*
*
* EntityRollingStock Overrides
*/

@Override
public void onAssemble() {
super.onAssemble();
Expand All @@ -52,7 +58,7 @@ public void onAssemble() {
}
initContainerFilter();
}

@Override
public void onDissassemble() {
super.onDissassemble();
Expand All @@ -75,7 +81,6 @@ public ClickResult onClick(Player player, Player.Hand hand) {
if (clickRes != ClickResult.PASS) {
return clickRes;
}

if (!this.isBuilt()) {
return ClickResult.PASS;
}
Expand Down Expand Up @@ -121,7 +126,52 @@ public ClickResult onClick(Player player, Player.Hand hand) {
return ClickResult.PASS;
}

protected boolean openGui(Player player) {
@Override
public void onTick() {
super.onTick();
ticks++;
FreightModel<?, ?> model = (FreightModel<?, ?>) this.getDefinition().getModel();
if(getWorld().isServer){
//inputs
if (this.getCurrentSpeed().metric() <= 10.8 && ticks % 2 == 0) {//3m/s and don't refresh it every tick
double temp = this.getDefinition().getLength(this.gauge) / 2;
Vec3d bound = new Vec3d(temp, temp, temp);
List<ItemStack> stacks = model.getItemsWithin(this, getWorld().getItemEntitiesWithinBB(IBoundingBox.from(
this.getPosition().add(bound), this.getPosition().subtract(bound))));

if (!stacks.isEmpty()) {
//transfer to this.cargoItems
for (int fromSlot = 0; fromSlot < stacks.size(); fromSlot++) {
ItemStack stack = stacks.get(fromSlot);
int origCount = stack.getCount();

if (stack.isEmpty()) {
continue;
}

for (int toSlot = 0; toSlot < this.cargoItems.getSlotCount(); toSlot++) {
stack.setCount(this.cargoItems.insert(toSlot, stack, false).getCount());
if (stack.isEmpty()) {
break;
}
}

if (origCount != stack.getCount()) {
stacks.set(fromSlot, stack);
}
}
}
}
//outputs
if (model.getUnloadingPoints() != null) {
for (CargoUnload point : model.getUnloadingPoints()) {
point.tryToUnload(this);
}
}
}
}

protected boolean openGui(Player player) {
if (getInventorySize() == 0) {
return false;
}
Expand All @@ -146,13 +196,13 @@ protected void handleMass() {
itemCount = itemInsideCount;
percentFull = this.getInventorySize() > 0 ? stacksWithStuff * 100 / this.getInventorySize() : 100;
}

public int getPercentCargoFull() {
return percentFull;
}

protected void initContainerFilter() {

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public void onDragRelease(Control<?> control) {
}

@Override
protected float defaultControlPosition(Control<?> control) {
public float defaultControlPosition(Control<?> control) {
switch (control.part.type) {
case THROTTLE_BRAKE_X:
case REVERSER_X:
Expand Down
Loading