From 775d17e9dbe04f4ef26d6757a035a161cdec80e1 Mon Sep 17 00:00:00 2001 From: yueh Date: Thu, 8 Dec 2016 17:59:29 +0100 Subject: [PATCH] Started P2P rework Changed frequency to short. Added a per grid RNG to request a new frequency, hopefully without many collisons. Added a helper to convert between a frequency and 4 colours --- src/main/java/appeng/me/cache/P2PCache.java | 53 +++++++++++++------ .../java/appeng/parts/p2p/PartP2PTunnel.java | 28 +++++----- .../java/appeng/spatial/StorageHelper.java | 2 + src/main/java/appeng/util/Platform.java | 19 +++++-- 4 files changed, 68 insertions(+), 34 deletions(-) diff --git a/src/main/java/appeng/me/cache/P2PCache.java b/src/main/java/appeng/me/cache/P2PCache.java index 43fe5c12..c9e654c7 100644 --- a/src/main/java/appeng/me/cache/P2PCache.java +++ b/src/main/java/appeng/me/cache/P2PCache.java @@ -20,8 +20,10 @@ package appeng.me.cache; import java.util.HashMap; +import java.util.Random; import com.google.common.collect.LinkedHashMultimap; +import com.google.common.collect.Maps; import com.google.common.collect.Multimap; import appeng.api.networking.GridFlags; @@ -34,22 +36,26 @@ import appeng.api.networking.events.MENetworkBootingStatusChange; import appeng.api.networking.events.MENetworkEventSubscribe; import appeng.api.networking.events.MENetworkPowerStatusChange; import appeng.api.networking.ticking.ITickManager; +import appeng.core.AELog; import appeng.me.cache.helpers.TunnelCollection; import appeng.parts.p2p.PartP2PTunnel; import appeng.parts.p2p.PartP2PTunnelME; +import appeng.util.Platform; public class P2PCache implements IGridCache { + private static final TunnelCollection NULL_COLLECTION = new TunnelCollection( null, null ); private final IGrid myGrid; - private final HashMap inputs = new HashMap(); - private final Multimap outputs = LinkedHashMultimap.create(); - private final TunnelCollection NullColl = new TunnelCollection( null, null ); + private final HashMap inputs = Maps.newHashMap(); + private final Multimap outputs = LinkedHashMultimap.create(); + private final Random frequencyGenerator; public P2PCache( final IGrid g ) { this.myGrid = g; + this.frequencyGenerator = new Random( g.hashCode() ); } @MENetworkEventSubscribe @@ -98,8 +104,7 @@ public class P2PCache implements IGridCache } final PartP2PTunnel t = (PartP2PTunnel) machine; - // AELog.info( "rmv-" + (t.output ? "output: " : "input: ") + t.freq - // ); + // AELog.info( "rmv-" + (t.output ? "output: " : "input: ") + t.freq ); if( t.isOutput() ) { @@ -128,8 +133,7 @@ public class P2PCache implements IGridCache } final PartP2PTunnel t = (PartP2PTunnel) machine; - // AELog.info( "add-" + (t.output ? "output: " : "input: ") + t.freq - // ); + // AELog.info( "add-" + (t.output ? "output: " : "input: ") + t.freq ); if( t.isOutput() ) { @@ -162,7 +166,7 @@ public class P2PCache implements IGridCache } - private void updateTunnel( final long freq, final boolean updateOutputs, final boolean configChange ) + private void updateTunnel( final short freq, final boolean updateOutputs, final boolean configChange ) { for( final PartP2PTunnel p : this.outputs.get( freq ) ) { @@ -184,7 +188,7 @@ public class P2PCache implements IGridCache } } - public void updateFreq( final PartP2PTunnel t, final long newFrequency ) + public void updateFreq( final PartP2PTunnel t, final short newFrequency ) { if( this.outputs.containsValue( t ) ) { @@ -207,30 +211,49 @@ public class P2PCache implements IGridCache this.inputs.put( t.getFrequency(), t ); } - // AELog.info( "update-" + (t.output ? "output: " : "input: ") + t.freq - // ); + // AELog.info( "update-" + (t.output ? "output: " : "input: ") + t.freq ); this.updateTunnel( t.getFrequency(), t.isOutput(), true ); this.updateTunnel( t.getFrequency(), !t.isOutput(), true ); } - public TunnelCollection getOutputs( final long freq, final Class c ) + public short newFrequency() + { + short newFrequency; + int cycles = 0; + + do + { + newFrequency = (short) this.frequencyGenerator.nextInt( 1 << 16 ); + cycles++; + } + while( newFrequency == 0 || this.inputs.containsKey( newFrequency ) ); + + if( cycles > 10 ) + { + AELog.debug( "Generating a new P2P frequency '%1$d' took %2$d cycles", newFrequency, cycles ); + } + + return newFrequency; + } + + public TunnelCollection getOutputs( final short freq, final Class c ) { final PartP2PTunnel in = this.inputs.get( freq ); if( in == null ) { - return this.NullColl; + return NULL_COLLECTION; } final TunnelCollection out = this.inputs.get( freq ).getCollection( this.outputs.get( freq ), c ); if( out == null ) { - return this.NullColl; + return NULL_COLLECTION; } return out; } - public PartP2PTunnel getInput( final long freq ) + public PartP2PTunnel getInput( final short freq ) { return this.inputs.get( freq ); } diff --git a/src/main/java/appeng/parts/p2p/PartP2PTunnel.java b/src/main/java/appeng/parts/p2p/PartP2PTunnel.java index 9cb1cef0..ef06a552 100644 --- a/src/main/java/appeng/parts/p2p/PartP2PTunnel.java +++ b/src/main/java/appeng/parts/p2p/PartP2PTunnel.java @@ -55,7 +55,7 @@ public abstract class PartP2PTunnel extends PartBasicSt { private final TunnelCollection type = new TunnelCollection( null, this.getClass() ); private boolean output; - private long freq; + private short freq; public PartP2PTunnel( final ItemStack is ) { @@ -134,7 +134,7 @@ public abstract class PartP2PTunnel extends PartBasicSt { super.readFromNBT( data ); this.setOutput( data.getBoolean( "output" ) ); - this.setFrequency( data.getLong( "freq" ) ); + this.setFrequency( data.getShort( "freq" ) ); } @Override @@ -142,7 +142,7 @@ public abstract class PartP2PTunnel extends PartBasicSt { super.writeToNBT( data ); data.setBoolean( "output", this.isOutput() ); - data.setLong( "freq", this.getFrequency() ); + data.setShort( "freq", this.getFrequency() ); } @Override @@ -172,7 +172,7 @@ public abstract class PartP2PTunnel extends PartBasicSt final NBTTagCompound data = mc.getData( is ); final ItemStack newType = ItemStack.loadItemStackFromNBT( data ); - final long freq = data.getLong( "freq" ); + final short freq = data.getShort( "freq" ); if( newType != null ) { @@ -268,7 +268,7 @@ public abstract class PartP2PTunnel extends PartBasicSt if( newType != null && !Platform.itemComparisons().isEqualItem( newType, this.getItemStack() ) ) { final boolean oldOutput = this.isOutput(); - final long myFreq = this.getFrequency(); + final short myFreq = this.getFrequency(); this.getHost().removePart( this.getSide(), false ); final AEPartLocation dir = this.getHost().addPart( newType, this.getSide(), player, hand ); @@ -308,17 +308,17 @@ public abstract class PartP2PTunnel extends PartBasicSt final IMemoryCard mc = (IMemoryCard) is.getItem(); final NBTTagCompound data = new NBTTagCompound(); - long newFreq = this.getFrequency(); + short newFreq = this.getFrequency(); final boolean wasOutput = this.isOutput(); this.setOutput( false ); - if( wasOutput || this.getFrequency() == 0 ) - { - newFreq = System.currentTimeMillis(); - } - try { + if( wasOutput || this.getFrequency() == 0 ) + { + newFreq = this.getProxy().getP2P().newFrequency(); + } + this.getProxy().getP2P().updateFreq( this, newFreq ); } catch( final GridAccessException e ) @@ -332,7 +332,7 @@ public abstract class PartP2PTunnel extends PartBasicSt final String type = p2pItem.getUnlocalizedName(); p2pItem.writeToNBT( data ); - data.setLong( "freq", this.getFrequency() ); + data.setShort( "freq", this.getFrequency() ); mc.setMemoryCardContents( is, type + ".name", data ); mc.notifyUser( player, MemoryCardMessages.SETTINGS_SAVED ); @@ -364,12 +364,12 @@ public abstract class PartP2PTunnel extends PartBasicSt } } - public long getFrequency() + public short getFrequency() { return this.freq; } - public void setFrequency( final long freq ) + public void setFrequency( final short freq ) { this.freq = freq; } diff --git a/src/main/java/appeng/spatial/StorageHelper.java b/src/main/java/appeng/spatial/StorageHelper.java index aa339f0a..8ac4b156 100644 --- a/src/main/java/appeng/spatial/StorageHelper.java +++ b/src/main/java/appeng/spatial/StorageHelper.java @@ -138,6 +138,8 @@ public class StorageHelper passanger.startRiding( entity, true ); } } + + entity.worldObj.updateEntity( entity ); return entity; } diff --git a/src/main/java/appeng/util/Platform.java b/src/main/java/appeng/util/Platform.java index 5bd47526..5047fd99 100644 --- a/src/main/java/appeng/util/Platform.java +++ b/src/main/java/appeng/util/Platform.java @@ -19,7 +19,6 @@ package appeng.util; -import java.lang.reflect.Method; import java.security.InvalidParameterException; import java.text.DecimalFormat; import java.util.ArrayList; @@ -30,6 +29,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Random; import java.util.WeakHashMap; + import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -76,7 +76,6 @@ import net.minecraftforge.common.util.FakePlayerFactory; import net.minecraftforge.fml.common.FMLCommonHandler; import net.minecraftforge.fml.common.Loader; import net.minecraftforge.fml.common.ModContainer; -import net.minecraftforge.fml.relauncher.ReflectionHelper; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import net.minecraftforge.oredict.OreDictionary; @@ -130,6 +129,7 @@ import appeng.me.GridAccessException; import appeng.me.GridNode; import appeng.me.helpers.AENetworkProxy; import appeng.util.helpers.ItemComparisonHelper; +import appeng.util.helpers.P2PHelper; import appeng.util.item.AEItemStack; import appeng.util.item.AESharedNBT; import appeng.util.prioritylist.IPartitionList; @@ -158,12 +158,18 @@ public class Platform // private static Method getEntry; private static final ItemComparisonHelper ITEM_COMPARISON_HELPER = new ItemComparisonHelper(); + private static final P2PHelper P2P_HELPER = new P2PHelper(); public static ItemComparisonHelper itemComparisons() { return ITEM_COMPARISON_HELPER; } + public static P2PHelper p2p() + { + return P2P_HELPER; + } + public static Random getRandom() { return RANDOM_GENERATOR; @@ -543,7 +549,8 @@ public class Platform final double offset_x = ( getRandomInt() % 32 - 16 ) / 82; final double offset_y = ( getRandomInt() % 32 - 16 ) / 82; final double offset_z = ( getRandomInt() % 32 - 16 ) / 82; - final EntityItem ei = new EntityItem( w, 0.5 + offset_x + pos.getX(), 0.5 + offset_y + pos.getY(), 0.2 + offset_z + pos.getZ(), i.copy() ); + final EntityItem ei = new EntityItem( w, 0.5 + offset_x + pos.getX(), 0.5 + offset_y + pos.getY(), 0.2 + offset_z + pos.getZ(), i + .copy() ); w.spawnEntityInWorld( ei ); } } @@ -1494,7 +1501,8 @@ public class Platform if( AEConfig.instance().isFeatureEnabled( AEFeature.LOG_SECURITY_AUDITS ) ) { AELog.info( - "Audit: " + a_isSecure + " : " + b_isSecure + " @ " + a.getLastSecurityKey() + " vs " + b.getLastSecurityKey() + " & " + a.getPlayerID() + " vs " + b.getPlayerID() ); + "Audit: " + a_isSecure + " : " + b_isSecure + " @ " + a.getLastSecurityKey() + " vs " + b.getLastSecurityKey() + " & " + a + .getPlayerID() + " vs " + b.getPlayerID() ); } // can't do that son... @@ -1645,7 +1653,8 @@ public class Platform } } - final boolean checkFuzzy = ae_req.isOre() || providedTemplate.getItemDamage() == OreDictionary.WILDCARD_VALUE || providedTemplate.hasTagCompound() || providedTemplate.isItemStackDamageable(); + final boolean checkFuzzy = ae_req.isOre() || providedTemplate.getItemDamage() == OreDictionary.WILDCARD_VALUE || providedTemplate + .hasTagCompound() || providedTemplate.isItemStackDamageable(); if( items != null && checkFuzzy ) {