Skip to content

Commit

Permalink
API improvements
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
EternalBlueFlame committed Sep 4, 2024
1 parent 6109e87 commit 1b90d0e
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 49 deletions.
8 changes: 8 additions & 0 deletions src/main/java/ebf/tim/api/SkinRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@ public class SkinRegistry {
public static void addSkin(Class<? extends AbstractTrains> train, TransportSkin str){
Traincraft.instance.traincraftRegistry.addLivery(train,str.addr);
}

public static void addSkin(Class<? extends AbstractTrains> train, String modid,String addr,String[] bogieSkins,String name, String description){
Traincraft.instance.traincraftRegistry.addLivery(train,modid+":"+addr);
}

public static void addSkin(Class<? extends AbstractTrains> train, String modid,String addr,String bogieSkin,String name, String description){
Traincraft.instance.traincraftRegistry.addLivery(train,modid+":"+addr);
}
}
4 changes: 4 additions & 0 deletions src/main/java/ebf/tim/api/TransportSkin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;}
}
49 changes: 44 additions & 5 deletions src/main/java/train/common/api/AbstractTrains.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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};
}
Expand Down Expand Up @@ -811,4 +807,47 @@ public String getDefaultSkin(){


public ArrayList<double[]> 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;
}
}
58 changes: 39 additions & 19 deletions src/main/java/train/common/api/Locomotive.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
}

Expand All @@ -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)
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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())) {
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/train/common/api/TrainRecord.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -56,7 +59,7 @@ public List<String> getLiveries() {

public abstract double getBogieLocoPosition();

public abstract Class<Entity> getEntityClass();
public abstract Class<AbstractTrains> getEntityClass();

@Deprecated
public abstract String getAdditionnalTooltip();
Expand Down Expand Up @@ -179,7 +182,7 @@ public double getBogieLocoPosition() {
}

@Override
public Class<Entity> getEntityClass() {
public Class<AbstractTrains> getEntityClass() {
return entityClass;
}

Expand Down
21 changes: 21 additions & 0 deletions src/main/java/train/common/api/TrainSound.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
17 changes: 1 addition & 16 deletions src/main/java/train/common/core/handlers/EntityHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
31 changes: 27 additions & 4 deletions src/main/java/train/common/library/TraincraftRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -155,16 +156,38 @@ public void addLivery(Class<?> entityClass, String name){
}


public static void registerTransports(String MODID, List<AbstractTrains> 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() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/train/common/tile/TileCrafterTierI.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public void markDirty() {
crafterInventory[i] = null;
}

List<TierRecipe> recipes = TierRecipeManager.getInstance().getTierRecipeList(1);
List<TierRecipe> recipes = TierRecipeManager.getInstance().getTierRecipeList(this.Tier());
int count = 0;
for (TierRecipe recipe : recipes) {
ItemStack stack = recipe.hasComponents(crafterInventory);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/train/common/tile/TileCrafterTierII.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public boolean isItemValidForSlot(int i, ItemStack stack) {
if(i>9)
return false;

List<TierRecipe> recipeList = TierRecipeManager.getInstance().getTierRecipeList(this.Tier);
List<TierRecipe> recipeList = TierRecipeManager.getInstance().getTierRecipeList(this.Tier());
for(TierRecipe recipe : recipeList){
ItemStack stack2 = recipe.getInput().get(i);
if (stack2 != null && TierRecipe.areItemsIdentical(stack, stack2)) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/train/common/tile/TileCrafterTierIII.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public void markDirty() {
crafterInventory[i] = null;
}

List<TierRecipe> recipes = TierRecipeManager.getInstance().getTierRecipeList(3);
List<TierRecipe> recipes = TierRecipeManager.getInstance().getTierRecipeList(this.Tier());
int count = 0;
for (int j = 0; j < recipes.size(); j++) {
ItemStack stack = recipes.get(j).hasComponents(crafterInventory);
Expand Down

0 comments on commit 1b90d0e

Please sign in to comment.