Skip to content

Commit 7f59093

Browse files
committedApr 18, 2015
Finally Fixed Locomotive Relay thanks to @CovertJaguar and @fnuecke
Also slightly improved some relay code.
1 parent 5fddb6d commit 7f59093

File tree

5 files changed

+118
-73
lines changed

5 files changed

+118
-73
lines changed
 

Diff for: ‎src/main/java/pl/asie/computronics/integration/railcraft/IntegrationRailcraft.java

+5-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import cpw.mods.fml.common.registry.GameRegistry;
44
import net.minecraft.block.Block;
5+
import net.minecraftforge.common.MinecraftForge;
56
import net.minecraftforge.common.config.Configuration;
67
import pl.asie.computronics.block.BlockDigitalDetector;
78
import pl.asie.computronics.block.BlockDigitalReceiverBox;
@@ -20,6 +21,8 @@ public class IntegrationRailcraft {
2021
public ItemRelaySensor relaySensor;
2122
public Block digitalBox;
2223

24+
LocomotiveManager manager;
25+
2326
private static boolean isEnabled(Configuration config, String name, boolean def) {
2427
return config.get("enable.railcraft", name, def).getBoolean(def);
2528
}
@@ -34,7 +37,8 @@ public IntegrationRailcraft(Configuration config) {
3437
relaySensor = new ItemRelaySensor();
3538
GameRegistry.registerItem(relaySensor, "computronics.relaySensor");
3639

37-
//MinecraftForge.EVENT_BUS.register(this);
40+
manager = new LocomotiveManager();
41+
MinecraftForge.EVENT_BUS.register(manager);
3842
}
3943
if(isEnabled(config, "digitalReceiverBox", true)) {
4044
this.digitalBox = new BlockDigitalReceiverBox();
@@ -47,15 +51,4 @@ public IntegrationRailcraft(Configuration config) {
4751
GameRegistry.registerTileEntity(TileDigitalDetector.class, "computronics.detector");
4852
}
4953
}
50-
51-
/*@SubscribeEvent
52-
public void onChunkUnload(ChunkEvent.Unload e) {
53-
for(List entityList : e.getChunk().entityLists) {
54-
for(Object o : entityList) {
55-
if(o instanceof EntityLocomotiveElectric) {
56-
LinkageManager.instance().removeLinkageId((EntityLocomotiveElectric) o);
57-
}
58-
}
59-
}
60-
}*/
6154
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package pl.asie.computronics.integration.railcraft;
2+
3+
import com.google.common.collect.MapMaker;
4+
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
5+
import mods.railcraft.common.carts.EntityLocomotiveElectric;
6+
import net.minecraft.entity.item.EntityMinecart;
7+
import net.minecraft.util.MathHelper;
8+
import net.minecraft.world.ChunkCoordIntPair;
9+
import net.minecraftforge.common.MinecraftForge;
10+
import net.minecraftforge.event.entity.EntityEvent;
11+
import net.minecraftforge.event.entity.minecart.MinecartUpdateEvent;
12+
import pl.asie.computronics.Computronics;
13+
14+
import java.util.Map;
15+
import java.util.UUID;
16+
17+
/**
18+
* @author CovertJaguar, Vexatos
19+
*/
20+
public class LocomotiveManager {
21+
private final Map<UUID, EntityLocomotiveElectric> carts = new MapMaker().weakValues().makeMap();
22+
23+
public static LocomotiveManager instance() {
24+
return Computronics.railcraft.manager;
25+
}
26+
27+
public void removeLinkageId(EntityLocomotiveElectric loco) {
28+
this.carts.remove(this.getLinkageId(loco));
29+
}
30+
31+
public UUID getLinkageId(EntityLocomotiveElectric loco) {
32+
UUID id = loco.getPersistentID();
33+
if(!isUnloaded(loco)) {
34+
this.carts.put(id, loco);
35+
}
36+
return id;
37+
}
38+
39+
private void addLocomotive(EntityLocomotiveElectric loco) {
40+
UUID id = loco.getPersistentID();
41+
if(!isUnloaded(loco)) {
42+
this.carts.put(id, loco);
43+
}
44+
}
45+
46+
public EntityLocomotiveElectric getCartFromUUID(UUID id) {
47+
EntityLocomotiveElectric cart = this.carts.get(id);
48+
if(cart != null && isUnloaded(cart)) {
49+
this.carts.remove(id);
50+
return null;
51+
} else {
52+
return this.carts.get(id);
53+
}
54+
}
55+
56+
private boolean isUnloaded(EntityLocomotiveElectric cart) {
57+
if(cart == null || cart.isDead || cart.worldObj == null) {
58+
return true;
59+
}
60+
61+
int x = MathHelper.floor_double(cart.posX);
62+
int z = MathHelper.floor_double(cart.posZ);
63+
boolean isForced = cart.worldObj.getPersistentChunks().containsKey(new ChunkCoordIntPair(x >> 4, z >> 4));
64+
byte searchRange = isForced ? (byte) 0 : 32;
65+
boolean isLoaded = cart.worldObj.checkChunksExist(x - searchRange, 0, z - searchRange, x + searchRange, 0, z + searchRange);
66+
if(!isLoaded) {
67+
EntityEvent.CanUpdate event = new EntityEvent.CanUpdate(cart);
68+
MinecraftForge.EVENT_BUS.post(event);
69+
isLoaded = event.canUpdate;
70+
}
71+
return !isLoaded;
72+
}
73+
74+
@SubscribeEvent
75+
public void onMinecartUpdate(MinecartUpdateEvent event) {
76+
EntityMinecart cart = event.minecart;
77+
if(!(cart instanceof EntityLocomotiveElectric)) {
78+
return;
79+
}
80+
EntityLocomotiveElectric loco = (EntityLocomotiveElectric) cart;
81+
if(loco.isDead) {
82+
this.removeLinkageId(loco);
83+
} else {
84+
this.addLocomotive(loco);
85+
}
86+
}
87+
}

Diff for: ‎src/main/java/pl/asie/computronics/item/ItemRelaySensor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public boolean onLeftClickEntity(ItemStack stack, EntityPlayer player, Entity en
9797
TileLocomotiveRelay relay = (TileLocomotiveRelay) entity.worldObj.getTileEntity(x, y, z);
9898
EntityLocomotiveElectric loco = (EntityLocomotiveElectric) entity;
9999
if(loco.dimension == relay.getWorldObj().provider.dimensionId) {
100-
if(loco.getDistance(relay.xCoord, relay.yCoord, relay.zCoord) <= Config.LOCOMOTIVE_RELAY_RANGE) {
100+
if(loco.getDistanceSq(relay.xCoord, relay.yCoord, relay.zCoord) <= Config.LOCOMOTIVE_RELAY_RANGE * Config.LOCOMOTIVE_RELAY_RANGE) {
101101
relay.setLocomotive(loco);
102102
player.addChatComponentMessage(new ChatComponentTranslation("chat.computronics.sensor.bound"));
103103
player.swingItem();

Diff for: ‎src/main/java/pl/asie/computronics/tile/TileLocomotiveRelay.java

+21-59
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@
77
import li.cil.oc.api.machine.Arguments;
88
import li.cil.oc.api.machine.Callback;
99
import li.cil.oc.api.machine.Context;
10-
import mods.railcraft.api.carts.CartTools;
1110
import mods.railcraft.common.carts.EntityLocomotiveElectric;
1211
import mods.railcraft.common.items.ItemTicket;
1312
import mods.railcraft.common.items.ItemTicketGold;
14-
import mods.railcraft.common.util.misc.ChunkManager;
1513
import mods.railcraft.common.util.misc.MiscTools;
16-
import net.minecraft.entity.item.EntityMinecart;
1714
import net.minecraft.item.ItemStack;
1815
import net.minecraft.nbt.NBTTagCompound;
19-
import net.minecraft.world.ChunkCache;
16+
import pl.asie.computronics.integration.railcraft.LocomotiveManager;
2017
import pl.asie.computronics.reference.Config;
2118
import pl.asie.computronics.reference.Mods;
2219

@@ -29,8 +26,8 @@
2926
public class TileLocomotiveRelay extends TileEntityPeripheralBase {
3027

3128
private WeakReference<EntityLocomotiveElectric> locomotive;
32-
private boolean isInitialized = false, isBound = false;
33-
//private int prevLocoticksExisted;
29+
//private boolean isInitialized = false;
30+
private boolean isBound = false;
3431

3532
private UUID uuid;
3633

@@ -39,20 +36,13 @@ public TileLocomotiveRelay() {
3936
}
4037

4138
public void setLocomotive(EntityLocomotiveElectric loco) {
42-
//this.locomotive = new WeakReference<EntityLocomotiveElectric>(loco);
39+
this.locomotive = new WeakReference<EntityLocomotiveElectric>(loco);
4340
this.isBound = true;
44-
this.uuid = loco.getUniqueID();
41+
this.uuid = loco.getPersistentID();
4542
}
4643

4744
public EntityLocomotiveElectric getLocomotive() {
48-
//return this.locomotive != null ? this.locomotive.get() : null;
49-
if(uuid != null) {
50-
EntityMinecart cart = CartTools.getLinkageManager(worldObj).getCartFromUUID(uuid);
51-
if(cart != null && cart instanceof EntityLocomotiveElectric) {
52-
return (EntityLocomotiveElectric) cart;
53-
}
54-
}
55-
return null;
45+
return this.locomotive != null ? this.locomotive.get() : null;
5646
}
5747

5848
public boolean isBound() {
@@ -72,61 +62,34 @@ public boolean unbind() {
7262
@Override
7363
public void updateEntity() {
7464
super.updateEntity();
65+
7566
if(worldObj.isRemote) {
7667
return;
7768
}
78-
if(!isInitialized && isBound && uuid != null) {
79-
this.tryFindLocomotive(this.uuid);
80-
isInitialized = true;
81-
}
8269

83-
EntityLocomotiveElectric locomotive = getLocomotive();
84-
85-
boolean b = true, c = true;
86-
87-
if(locomotive != null && locomotive.dimension == worldObj.provider.dimensionId) {
88-
b = locomotive.worldObj.getChunkProvider().chunkExists(locomotive.chunkCoordX, locomotive.chunkCoordZ);
89-
if(!b) {
90-
return;
91-
}
92-
c = locomotive.worldObj.getChunkFromChunkCoords(locomotive.chunkCoordX, locomotive.chunkCoordZ).isChunkLoaded;
93-
}
94-
if(locomotive != null && (locomotive.isDead || !isBound
95-
|| !locomotive.worldObj.getChunkProvider().chunkExists(locomotive.chunkCoordX, locomotive.chunkCoordZ))) {
96-
this.locomotive = null;
97-
//locomotive = null;
70+
if(!isBound) {
9871
return;
9972
}
10073

101-
if(isBound && uuid == null) {
74+
if(uuid == null) {
10275
isBound = false;
10376
}
10477

105-
if(locomotive != null && locomotive.dimension != worldObj.provider.dimensionId) {
106-
return;
107-
}
108-
109-
/*if(locomotive != null) {
110-
prevLocoticksExisted = locomotive.ticksExisted;
111-
}*/
112-
113-
if(locomotive != null || !isBound) {
114-
return;
115-
}
116-
// Only check every second
117-
if(worldObj.getTotalWorldTime() % 20 == 0) {
78+
if(uuid != null) {
11879
this.tryFindLocomotive(this.uuid);
11980
}
12081
}
12182

12283
private void tryFindLocomotive(UUID uuid) {
123-
if(getLocomotive() != null) {
124-
return;
125-
}
12684
if(uuid != null) {
127-
EntityMinecart cart = CartTools.getLinkageManager(worldObj).getCartFromUUID(uuid);
128-
if(cart != null && cart instanceof EntityLocomotiveElectric) {
129-
this.setLocomotive((EntityLocomotiveElectric) cart);
85+
EntityLocomotiveElectric cart = LocomotiveManager.instance().getCartFromUUID(uuid);
86+
if(cart != null) {
87+
EntityLocomotiveElectric oldLoco = getLocomotive();
88+
if(oldLoco == null || cart != oldLoco) {
89+
this.setLocomotive(cart);
90+
}
91+
} else {
92+
this.locomotive = null;
13093
}
13194
}
13295
}
@@ -143,9 +106,8 @@ public void readFromNBT(NBTTagCompound nbt) {
143106
@Override
144107
public void writeToNBT(NBTTagCompound nbt) {
145108
super.writeToNBT(nbt);
146-
EntityLocomotiveElectric locomotive = getLocomotive();
147-
if(isBound && locomotive != null) {
148-
MiscTools.writeUUID(nbt, "locomotive", locomotive.getPersistentID());
109+
if(isBound && this.uuid != null) {
110+
MiscTools.writeUUID(nbt, "locomotive", this.uuid);
149111
}
150112
nbt.setBoolean("bound", isBound);
151113
}
@@ -175,7 +137,7 @@ private String cannotAccessLocomotive() {
175137
if(locomotive.dimension != this.worldObj.provider.dimensionId) {
176138
return "relay and locomotive are in different dimensions";
177139
}
178-
if(!(locomotive.getDistance(xCoord, yCoord, zCoord) <= Config.LOCOMOTIVE_RELAY_RANGE)) {
140+
if(locomotive.getDistanceSq(xCoord, yCoord, zCoord) > Config.LOCOMOTIVE_RELAY_RANGE * Config.LOCOMOTIVE_RELAY_RANGE) {
179141
return "locomotive is too far away";
180142
}
181143
if(locomotive.isSecure()) {

Diff for: ‎src/main/java/pl/asie/computronics/util/achievements/ComputronicsAchievements.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,15 @@ private static void onLeftClickEntity(AttackEntityEvent event) {
242242
int x = data.getInteger("relayX");
243243
int y = data.getInteger("relayY");
244244
int z = data.getInteger("relayZ");
245+
if(!player.worldObj.blockExists(x, y, z)) {
246+
return;
247+
}
245248
if(loco.worldObj.getTileEntity(x, y, z) != null
246249
&& loco.worldObj.getTileEntity(x, y, z) instanceof TileLocomotiveRelay) {
247250

248251
TileLocomotiveRelay relay = (TileLocomotiveRelay) loco.worldObj.getTileEntity(x, y, z);
249252
if(loco.dimension == relay.getWorldObj().provider.dimensionId
250-
&& loco.getDistance(relay.xCoord, relay.yCoord, relay.zCoord) <= Config.LOCOMOTIVE_RELAY_RANGE) {
253+
&& loco.getDistanceSq(relay.xCoord, relay.yCoord, relay.zCoord) <= Config.LOCOMOTIVE_RELAY_RANGE * Config.LOCOMOTIVE_RELAY_RANGE) {
251254

252255
Computronics.instance.achievements.triggerAchievement(player, EnumAchievements.Relay);
253256
}

0 commit comments

Comments
 (0)
Please sign in to comment.