From 1b90d0eb08ae9c90b481c6eaea766ab1174c64c5 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Tue, 3 Sep 2024 20:09:23 -0500 Subject: [PATCH] API improvements - Added SkinRegistry endpoints to match some that TiM had. not fully used yet, will be later. - Added the options for post-process recolors to skins, not used yet, but will be eventually. - Sound rework to add additional caching and help prevent bell/whistle spam, sounds can also now be defined from the entity itself without needing to mess with an enum. - Trains should now show as powered minecarts if anything checks it. - new registration option added to the TraincraftRegistry that accepts a TrainRecord. - TraincraftRegistry transport registration methods can now use the `getRecipe()` and `getTier()` methods to automatically make recipes for trains and stock. - Tier crafters updated, you should be able to extend them and change the tier value to make your own craft tier table. --- src/main/java/ebf/tim/api/SkinRegistry.java | 8 +++ src/main/java/ebf/tim/api/TransportSkin.java | 4 ++ .../java/train/common/api/AbstractTrains.java | 49 ++++++++++++++-- .../java/train/common/api/Locomotive.java | 58 +++++++++++++------ .../java/train/common/api/TrainRecord.java | 7 ++- .../java/train/common/api/TrainSound.java | 21 +++++++ .../common/core/handlers/EntityHandler.java | 17 +----- .../common/library/TraincraftRegistry.java | 31 ++++++++-- .../train/common/tile/TileCrafterTierI.java | 2 +- .../train/common/tile/TileCrafterTierII.java | 2 +- .../train/common/tile/TileCrafterTierIII.java | 2 +- 11 files changed, 152 insertions(+), 49 deletions(-) create mode 100644 src/main/java/train/common/api/TrainSound.java diff --git a/src/main/java/ebf/tim/api/SkinRegistry.java b/src/main/java/ebf/tim/api/SkinRegistry.java index 9dcb47904..e7332f47e 100644 --- a/src/main/java/ebf/tim/api/SkinRegistry.java +++ b/src/main/java/ebf/tim/api/SkinRegistry.java @@ -9,4 +9,12 @@ public class SkinRegistry { public static void addSkin(Class train, TransportSkin str){ Traincraft.instance.traincraftRegistry.addLivery(train,str.addr); } + + public static void addSkin(Class train, String modid,String addr,String[] bogieSkins,String name, String description){ + Traincraft.instance.traincraftRegistry.addLivery(train,modid+":"+addr); + } + + public static void addSkin(Class train, String modid,String addr,String bogieSkin,String name, String description){ + Traincraft.instance.traincraftRegistry.addLivery(train,modid+":"+addr); + } } diff --git a/src/main/java/ebf/tim/api/TransportSkin.java b/src/main/java/ebf/tim/api/TransportSkin.java index 6abf5fc00..55136f5ca 100644 --- a/src/main/java/ebf/tim/api/TransportSkin.java +++ b/src/main/java/ebf/tim/api/TransportSkin.java @@ -3,4 +3,8 @@ public class TransportSkin { String addr; public TransportSkin(String modID, String a, String b, String c){ addr= b;} + @Deprecated//todo! + public TransportSkin setRecolorsFrom(int i){return this;} + @Deprecated//todo! + public TransportSkin setRecolorsTo(int i){return this;} } diff --git a/src/main/java/train/common/api/AbstractTrains.java b/src/main/java/train/common/api/AbstractTrains.java index 3db9e59ee..6d3054648 100644 --- a/src/main/java/train/common/api/AbstractTrains.java +++ b/src/main/java/train/common/api/AbstractTrains.java @@ -18,10 +18,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; -import net.minecraft.util.AxisAlignedBB; -import net.minecraft.util.ChatComponentText; -import net.minecraft.util.DamageSource; -import net.minecraft.util.StatCollector; +import net.minecraft.util.*; import net.minecraft.world.ChunkCoordIntPair; import net.minecraft.world.World; import net.minecraftforge.common.ForgeChunkManager; @@ -748,7 +745,6 @@ public String transportFuelType(){ * example: * return new Bogie[]{new Bogie(new MyModel1(), offset), new Bogie(new MyModel2(), offset2), etc...}; * may return null. */ - @SideOnly(Side.CLIENT) public Bogie[] bogies(){ return new Bogie[]{null}; } @@ -811,4 +807,47 @@ public String getDefaultSkin(){ public ArrayList getSmokePosition() {return null;} + + public int[] getParticleData(int id) { + switch (id){ + case 1: {return new int[]{1,200,0xFF0000};} + default: {return new int[]{1,10,0xCCCC11};} + } + } + + public ItemStack[] getRecipe(){return null;} + public int getTier(){return 1;} + + public TrainSound getHorn(){ + TrainSoundRecord sound = Traincraft.instance.traincraftRegistry.getTrainSoundRecord(this.getClass()); + if(sound != null && !sound.getHornString().isEmpty()){ + return new TrainSound(sound.getHornString(),sound.getHornVolume(),1f, 0); + } + return null; + } + + public TrainSound getBell(){ + return new TrainSound(Info.resourceLocation + ":bell",0.5f,1f, 0); + } + + public TrainSound getRunningSound(){ + TrainSoundRecord sound = Traincraft.instance.traincraftRegistry.getTrainSoundRecord(this.getClass()); + if(sound != null && !sound.getRunString().isEmpty()){ + if(sound.getSoundChangeWithSpeed()){ + return new TrainSound(sound.getRunString(), sound.getRunVolume(), 0.4f, sound.getRunSoundLength()).enableRunningPitch(); + } else { + return new TrainSound(sound.getRunString(), sound.getRunVolume(), 0.4f, sound.getRunSoundLength()); + } + } + return null; + } + + + public TrainSound getIdleSound(){ + TrainSoundRecord sound = Traincraft.instance.traincraftRegistry.getTrainSoundRecord(this.getClass()); + if(sound != null && !sound.getIdleString().isEmpty()){ + return new TrainSound(sound.getIdleString(),sound.getIdleVolume(),0.001F, sound.getIdleSoundLength()); + } + return null; + } } \ No newline at end of file diff --git a/src/main/java/train/common/api/Locomotive.java b/src/main/java/train/common/api/Locomotive.java index 41ce41b8c..31d7e66bf 100644 --- a/src/main/java/train/common/api/Locomotive.java +++ b/src/main/java/train/common/api/Locomotive.java @@ -58,7 +58,7 @@ public abstract class Locomotive extends EntityRollingStock implements IInventor private int slotsFilled = 0; public boolean isLocoTurnedOn = false; public boolean forwardPressed = false; - private boolean backwardPressed = false; + public boolean backwardPressed = false; public boolean brakePressed = false; public int speedLimit = 0; @@ -100,7 +100,10 @@ public abstract class Locomotive extends EntityRollingStock implements IInventor private byte ditchLightMode = 0; private byte beaconCycleIndex = 0; - public TrainSoundRecord sound = Traincraft.instance.traincraftRegistry.getTrainSoundRecord(this.getClass()); + public TrainSound soundRunning; + public TrainSound soundIdle; + public TrainSound soundHorn; + public TrainSound soundBell; /** * state of the loco */ @@ -593,9 +596,11 @@ private double convertSpeed(double speed) { } public void soundHorn() { - TrainSoundRecord sound = Traincraft.instance.traincraftRegistry.getTrainSoundRecord(this.getClass()); - if (sound != null && !sound.getHornString().isEmpty() && whistleDelay == 0) { - worldObj.playSoundAtEntity(this, sound.getHornString(), sound.getHornVolume(), 1.0F); + if(soundHorn==null){ + soundHorn=getHorn(); + } + if (soundHorn != null && !soundHorn.addr.isEmpty() && whistleDelay == 0) { + worldObj.playSoundAtEntity(this, soundHorn.addr, soundHorn.vol, soundHorn.pit); whistleDelay = 65; } @@ -612,8 +617,13 @@ public void soundHorn() { } public void soundWhistle() { - worldObj.playSoundAtEntity(this, Info.resourceLocation + ":" + "bell", 0.5F, 1.0F); - + if(soundBell==null){ + soundBell=getBell(); + } + if (soundBell != null && !soundBell.addr.isEmpty() && whistleDelay == 0) { + worldObj.playSoundAtEntity(this, soundBell.addr, soundBell.vol, soundBell.pit); + whistleDelay = 65; + } } @SideOnly(Side.CLIENT) @@ -755,29 +765,35 @@ public void onUpdate() motionZ *= 0.0; } if (ConfigHandler.SOUNDS && whistleDelay == 0) { - if (sound != null && !sound.getHornString().isEmpty()) { + if(soundIdle==null){ + soundIdle=getIdleSound(); + } + if (soundIdle != null && !soundIdle.addr.isEmpty()) { if (getFuel() > 0 && this.isLocoTurnedOn()) { double speed = Math.sqrt(motionX * motionX + motionZ * motionZ); if (speed > -0.001D && speed < 0.01D && soundPosition == 0) { - worldObj.playSoundAtEntity(this, sound.getIdleString(), sound.getIdleVolume(), 0.001F); - soundPosition = sound.getIdleSoundLength(); + worldObj.playSoundAtEntity(this, soundIdle.addr, soundIdle.vol, soundIdle.pit); + soundPosition = soundIdle.len; } - if (sound.getSoundChangeWithSpeed() && !sound.getHornString().isEmpty() && whistleDelay == 0) { + if(soundRunning==null){ + soundRunning=getRunningSound(); + } + if (soundRunning!=null && soundRunning.runningPitch && !soundRunning.addr.isEmpty() && whistleDelay == 0) { if (speed > 0.01D && speed < 0.06D && soundPosition == 0) { - worldObj.playSoundAtEntity(this, sound.getRunString(), sound.getRunVolume(), 0.1F); - soundPosition = sound.getRunSoundLength(); + worldObj.playSoundAtEntity(this, soundRunning.addr, soundRunning.vol, soundRunning.pit-0.3f); + soundPosition = soundRunning.len; } else if (speed > 0.06D && speed < 0.2D && soundPosition == 0) { - worldObj.playSoundAtEntity(this, sound.getRunString(), sound.getRunVolume(), 0.4F); - soundPosition = sound.getRunSoundLength() / 2; + worldObj.playSoundAtEntity(this, soundRunning.addr, soundRunning.vol, soundRunning.pit-0.1f); + soundPosition = soundRunning.len / 2; } else if (speed > 0.2D && soundPosition == 0) { - worldObj.playSoundAtEntity(this, sound.getRunString(), sound.getRunVolume(), 0.5F); - soundPosition = sound.getRunSoundLength() / 3; + worldObj.playSoundAtEntity(this, soundRunning.addr, soundRunning.vol, soundRunning.pit); + soundPosition = soundRunning.len / 3; } } else { if (speed > 0.01D && soundPosition == 0) { - worldObj.playSoundAtEntity(this, sound.getRunString(), sound.getRunVolume(), 0.4F); - soundPosition = sound.getRunSoundLength(); + worldObj.playSoundAtEntity(this, soundRunning.addr, soundRunning.vol, soundRunning.pit); + soundPosition = soundRunning.len; } } @@ -988,6 +1004,10 @@ public void onUpdate() } } + @Override + public int getMinecartType() { + return 2; + } public boolean isNotOwner() { if (this.riddenByEntity instanceof EntityPlayer && !((EntityPlayer) this.riddenByEntity).getDisplayName().equalsIgnoreCase(this.getTrainOwner())) { diff --git a/src/main/java/train/common/api/TrainRecord.java b/src/main/java/train/common/api/TrainRecord.java index 5a3627ec8..f0de56b31 100644 --- a/src/main/java/train/common/api/TrainRecord.java +++ b/src/main/java/train/common/api/TrainRecord.java @@ -1,9 +1,12 @@ package train.common.api; +import cpw.mods.fml.common.registry.EntityRegistry; import ebf.tim.utility.DebugUtil; import net.minecraft.entity.Entity; import net.minecraft.item.Item; import net.minecraft.world.World; +import train.common.Traincraft; +import train.common.core.managers.TierRecipeManager; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; @@ -56,7 +59,7 @@ public List getLiveries() { public abstract double getBogieLocoPosition(); - public abstract Class getEntityClass(); + public abstract Class getEntityClass(); @Deprecated public abstract String getAdditionnalTooltip(); @@ -179,7 +182,7 @@ public double getBogieLocoPosition() { } @Override - public Class getEntityClass() { + public Class getEntityClass() { return entityClass; } diff --git a/src/main/java/train/common/api/TrainSound.java b/src/main/java/train/common/api/TrainSound.java new file mode 100644 index 000000000..a4b8b7bcd --- /dev/null +++ b/src/main/java/train/common/api/TrainSound.java @@ -0,0 +1,21 @@ +package train.common.api; + +public class TrainSound { + public String addr; + public float vol=1; + public float pit=1; + public int len=1; + public boolean runningPitch=false; + + public TrainSound(String address, float volume, float pitch, int length){ + addr=address; + vol=volume; + pit=pitch; + len=length; + } + public TrainSound enableRunningPitch(){ + runningPitch=true; + return this; + } + +} diff --git a/src/main/java/train/common/core/handlers/EntityHandler.java b/src/main/java/train/common/core/handlers/EntityHandler.java index d538ea859..2c9e15c0f 100644 --- a/src/main/java/train/common/core/handlers/EntityHandler.java +++ b/src/main/java/train/common/core/handlers/EntityHandler.java @@ -20,27 +20,12 @@ import train.common.library.EnumTrains; public class EntityHandler { - public static int trainID= 32; public static void init() { EntityRegistry.registerModEntity(EntityZeppelinTwoBalloons.class, "zeppelin", EntityIds.ZEPPELIN, Traincraft.instance, 512, 1, true);//zepplin EntityRegistry.registerModEntity(EntityBogie.class, "Entity Front Bogie", EntityIds.LOCOMOTIVE_BOGIE, Traincraft.instance, 512, 1, true);//front bogie EntityRegistry.registerModEntity(EntityZeppelinOneBalloon.class, "zeppelin big", EntityIds.ZEPPELIN_BIG, Traincraft.instance, 512, 1, true);//zepplin big for(TrainRecord trains : EnumTrains.trains()){ - EntityRegistry.registerModEntity(trains.getEntityClass(), trains.getInternalName(), trainID, Traincraft.instance, 512, 1, true); - if(trains.getEntity(null)!=null) { - trains.getEntity(null).registerSkins(); - } - trainID++; - if(trainID== 112 || trainID==51){ - trainID++; - } + TrainRecord.registerTransport(trains); } } - - public static Entity getEntityServer(World world, int entityId) { - if ((world instanceof WorldServer)) { - return world.getEntityByID(entityId); - } - return null; - } } \ No newline at end of file diff --git a/src/main/java/train/common/library/TraincraftRegistry.java b/src/main/java/train/common/library/TraincraftRegistry.java index 182bef4dc..847cf9d46 100644 --- a/src/main/java/train/common/library/TraincraftRegistry.java +++ b/src/main/java/train/common/library/TraincraftRegistry.java @@ -46,6 +46,7 @@ import train.common.api.blocks.TileRenderFacing; import train.common.blocks.BlockTraincraftFluid; import train.common.core.handlers.EntityHandler; +import train.common.core.managers.TierRecipeManager; import train.common.items.ItemRollingStock; import javax.annotation.Nullable; @@ -155,16 +156,38 @@ public void addLivery(Class entityClass, String name){ } - public static void registerTransports(String MODID, List entities) { - registerTransports(MODID,entities.toArray(new AbstractTrains[]{})); + + public static int trainID= 32; + public static void registerTransport(TrainRecord record){ + EntityRegistry.registerModEntity(record.getEntityClass(), record.getInternalName(), trainID, Traincraft.instance, 512, 1, true); + AbstractTrains entity = record.getEntity(null); + if(entity!=null) { + entity.registerSkins(); + if(entity.getRecipe()!=null){ + TierRecipeManager.getInstance().addRecipe(entity.getTier(), + entity.getRecipe()[0],entity.getRecipe()[1],entity.getRecipe()[2],entity.getRecipe()[3], + entity.getRecipe()[4],entity.getRecipe()[5],entity.getRecipe()[6],entity.getRecipe()[7], + entity.getRecipe()[8],entity.getRecipe()[9], entity.getCartItem(),1); + } + } + trainID++; + if(trainID== 112 || trainID==51){ + trainID++; + } } public static void registerTransports(String MODID, AbstractTrains[] entities) { for(final AbstractTrains trains : entities){ - EntityRegistry.registerModEntity(trains.getClass(), MODID+":"+trains.transportName(), EntityHandler.trainID, Traincraft.instance, 512, 1, true); + EntityRegistry.registerModEntity(trains.getClass(), MODID+":"+trains.transportName(), trainID, Traincraft.instance, 512, 1, true); trains.registerSkins(); GameRegistry.registerItem(trains.getItem(), MODID+":entity/"+trains.transportName()); - EntityHandler.trainID+=1; + trainID+=1; + if(trains.getRecipe()!=null){ + TierRecipeManager.getInstance().addRecipe(trains.getTier(), + trains.getRecipe()[0],trains.getRecipe()[1],trains.getRecipe()[2],trains.getRecipe()[3], + trains.getRecipe()[4],trains.getRecipe()[5],trains.getRecipe()[6],trains.getRecipe()[7], + trains.getRecipe()[8],trains.getRecipe()[9], trains.getCartItem(),1); + } //todo:this part should be unnecessary? double-check. if(Traincraft.proxy.isClient()){ Traincraft.instance.traincraftRegistry.registerTrainRenderRecord(new TrainRenderRecord() { diff --git a/src/main/java/train/common/tile/TileCrafterTierI.java b/src/main/java/train/common/tile/TileCrafterTierI.java index bf6693f5e..5010154cf 100644 --- a/src/main/java/train/common/tile/TileCrafterTierI.java +++ b/src/main/java/train/common/tile/TileCrafterTierI.java @@ -196,7 +196,7 @@ public void markDirty() { crafterInventory[i] = null; } - List recipes = TierRecipeManager.getInstance().getTierRecipeList(1); + List recipes = TierRecipeManager.getInstance().getTierRecipeList(this.Tier()); int count = 0; for (TierRecipe recipe : recipes) { ItemStack stack = recipe.hasComponents(crafterInventory); diff --git a/src/main/java/train/common/tile/TileCrafterTierII.java b/src/main/java/train/common/tile/TileCrafterTierII.java index 653d3b5cd..c76bd27c5 100644 --- a/src/main/java/train/common/tile/TileCrafterTierII.java +++ b/src/main/java/train/common/tile/TileCrafterTierII.java @@ -289,7 +289,7 @@ public boolean isItemValidForSlot(int i, ItemStack stack) { if(i>9) return false; - List recipeList = TierRecipeManager.getInstance().getTierRecipeList(this.Tier); + List recipeList = TierRecipeManager.getInstance().getTierRecipeList(this.Tier()); for(TierRecipe recipe : recipeList){ ItemStack stack2 = recipe.getInput().get(i); if (stack2 != null && TierRecipe.areItemsIdentical(stack, stack2)) { diff --git a/src/main/java/train/common/tile/TileCrafterTierIII.java b/src/main/java/train/common/tile/TileCrafterTierIII.java index fdc050a5b..c52f181a7 100644 --- a/src/main/java/train/common/tile/TileCrafterTierIII.java +++ b/src/main/java/train/common/tile/TileCrafterTierIII.java @@ -165,7 +165,7 @@ public void markDirty() { crafterInventory[i] = null; } - List recipes = TierRecipeManager.getInstance().getTierRecipeList(3); + List recipes = TierRecipeManager.getInstance().getTierRecipeList(this.Tier()); int count = 0; for (int j = 0; j < recipes.size(); j++) { ItemStack stack = recipes.get(j).hasComponents(crafterInventory);