• Recently Browsing

    No registered users viewing this page.

  • Posts

    • Changes to the body of an existing method
    • werid, i fixed that a while ago: https://github.com/drmdgg/marijuanacraft1.14.4/blob/b9dabc49d8731443f42a680138ad3159e63d5bfb/src/main/java/drmdgg/marijuanacraft/network/PacketButtonClicked.java#L27
    • my code of Capability : package fr.mattmouss.switchrail.other; import fr.mattmouss.switchrail.blocks.SwitchTile; import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.INBT; import net.minecraft.nbt.ListNBT; import net.minecraft.util.Direction; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.CapabilityInject; import net.minecraftforge.common.capabilities.CapabilityManager; import javax.annotation.Nullable; import java.util.List; public class SwitchStorageCapability { @CapabilityInject(ISwitchStorage.class) public static Capability<ISwitchStorage> SWITCH_STORAGE_CAPABILITY = null ; public static void register() { CapabilityManager.INSTANCE.register(ISwitchStorage.class, new Capability.IStorage<ISwitchStorage>() { @Override public INBT writeNBT(Capability<ISwitchStorage> capability, ISwitchStorage instance, Direction side) { ListNBT switches = new ListNBT(); List<SwitchTile> switchList = instance.getSwitchList(); for (SwitchTile switch_tile : switchList){ CompoundNBT tag_switch = new CompoundNBT(); BlockPos pos =switch_tile.getPos(); tag_switch.putInt("posX",pos.getX()); tag_switch.putInt("posY",pos.getY()); tag_switch.putInt("posZ",pos.getZ()); tag_switch.putString("type",switch_tile.getBlockState().getBlock().getRegistryName().toString()); switches.add(tag_switch); } return switches; } @Override public void readNBT(Capability<ISwitchStorage> capability, ISwitchStorage instance, Direction side, INBT nbt) { if (!(instance instanceof SwitchStorage)) throw new IllegalArgumentException("Can not deserialize to an instance that isn't the default implementation"); ListNBT List_tag= (ListNBT)nbt; int max = List_tag.size(); for (int i=0;i<max;i++){ CompoundNBT tag_switch = List_tag.getCompound(i); int posX = tag_switch.getInt("posX"); int posY = tag_switch.getInt("posY"); int posZ = tag_switch.getInt("posZ"); World world = ((SwitchStorage)instance).getWorld(); SwitchTile switch_tile = (SwitchTile) world.getTileEntity(new BlockPos(posX,posY,posZ)); ((SwitchStorage)instance).addSwitch(switch_tile); } } },SwitchStorage::new); } }   My class SwitchStorage : package fr.mattmouss.switchrail.other; import fr.mattmouss.switchrail.blocks.SwitchTile; import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.ListNBT; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraftforge.common.util.INBTSerializable; import java.util.ArrayList; import java.util.List; public class SwitchStorage implements INBTSerializable<CompoundNBT>,ISwitchStorage{ private List<SwitchTile> switchList; private static World world; public SwitchStorage() { switchList = new ArrayList<>(); } public SwitchStorage(World world) { this.world = world; switchList = new ArrayList<>(); } @Override public CompoundNBT serializeNBT() { CompoundNBT tag = new CompoundNBT(); ListNBT switches = new ListNBT(); for (SwitchTile switch_tile : switchList){ CompoundNBT tag_switch = new CompoundNBT(); BlockPos pos =switch_tile.getPos(); tag_switch.putInt("posX",pos.getX()); tag_switch.putInt("posY",pos.getY()); tag_switch.putInt("posZ",pos.getZ()); tag_switch.putString("type",switch_tile.getBlockState().getBlock().getRegistryName().toString()); switches.add(tag_switch); } tag.put("switch_list",switches); return tag; } public List<SwitchTile> getSwitchList() { System.out.println("**SwitchStorage** nombre de switch enregistré :"+switchList.size()); return switchList; } public World getWorld() { return world; } @Override public void deserializeNBT(CompoundNBT nbt) { System.out.println("---------deserialise la liste----------"); ListNBT switch_List= (ListNBT) nbt.get("switch_list"); int max = switch_List.size(); for (int i=0;i<max;i++){ CompoundNBT tag_switch = switch_List.getCompound(i); int posX = tag_switch.getInt("posX"); int posY = tag_switch.getInt("posY"); int posZ = tag_switch.getInt("posZ"); SwitchTile switch_tile = (SwitchTile) world.getTileEntity(new BlockPos(posX,posY,posZ)); if (!switchList.contains(switch_tile)){ switchList.add(switch_tile); } } } public void addSwitch(SwitchTile switchTile){ if (isNotDoubloon(switchTile)){ switchList.add(switchTile); } else { System.out.println("**ERROR** : switch already registered !!"); } } public void deleteSwitch(SwitchTile switch_tile){ System.out.println("switch a supprimer :"+switch_tile); if (!isNotDoubloon(switch_tile)){ switchList.remove(switch_tile); } else { System.out.println("le block n'existe pas !!"); } } public Boolean isNotDoubloon(SwitchTile switchTile) { /* private boolean isNotDoubloon(SwitchTile switchTile) { AtomicReference<Boolean> result = null; switches.ifPresent(switchStorage -> result.set(switchStorage.isNotDoubloon(switchTile))); return result.get(); for (SwitchTile tile : switches){ BlockPos pos_in = tile.getPos(); if (pos_in.getX() == pos.getX() && pos_in.getY() == pos.getY() && pos_in.getZ() == pos.getZ()){ return false; } } return true; */ for (SwitchTile tile : switchList){ BlockPos pos = switchTile.getPos(); BlockPos pos_it = tile.getPos(); if (pos_it.getX() == pos.getX() && pos_it.getY() == pos.getY() && pos_it.getZ() == pos.getZ()){ return false; } } return true; } } In ISwitchStorage is just a couple of function And here is my Tile that use the capability : package fr.mattmouss.switchrail.blocks; public class ControllerTile extends TileEntity implements ITickableTileEntity, INamedContainerProvider { //ne pas utiliser de liste globale : il faut faire comme dans MyTutorial avec des LazyOptional et des read/write compoundNBT //problème observé : deux controleurs diffrents ont la même liste de switchs private LazyOptional<SwitchStorage> switches = LazyOptional.of(this::createSwitch).cast(); //private static List<SwitchTile> switches ; public ControllerTile() { super(ModBlock.CONTROLLER_TILE); } @Override public void tick() { } private SwitchStorage createSwitch(){ return new SwitchStorage(world); } public void openGui(){ ControllerScreen screen = new ControllerScreen(this); screen.init(); Minecraft.getInstance().displayGuiScreen(screen); } public void addSwitch(SwitchTile switch_tile){ switches.ifPresent(switchStorage -> switchStorage.addSwitch(switch_tile)); } public void deleteSwitch(SwitchTile switch_tile){ switches.ifPresent(switchStorage -> switchStorage.deleteSwitch(switch_tile)); } public List<SwitchTile> getSwitch() { List<SwitchTile> tiles = new ArrayList<>(); List<SwitchTile> tiles2 = getCapability(SwitchStorageCapability.SWITCH_STORAGE_CAPABILITY).map(ISwitchStorage::getSwitchList).orElse(tiles); System.out.println("**ControllerTile** nombre de switch enregistré :"+tiles2.size()); return tiles2; } @Override public ITextComponent getDisplayName() { return new StringTextComponent(getType().getRegistryName().getPath()); } @Nullable @Override public Container createMenu(int i, PlayerInventory inventory, PlayerEntity playerEntity) { return null; } @Override public void read(CompoundNBT compound) { CompoundNBT switch_tag = compound.getCompound("swag"); switches.ifPresent(switchStorage -> ((INBTSerializable<CompoundNBT>)switch_tag).deserializeNBT(switch_tag)); super.read(compound); } @Override public CompoundNBT write(CompoundNBT tag) { switches.ifPresent(switchStorage -> { CompoundNBT compoundNBT = ((INBTSerializable<CompoundNBT>)switchStorage).serializeNBT(); tag.put("swag",compoundNBT); }); return super.write(tag); } @Nonnull @Override public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) { if (cap == SwitchStorageCapability.SWITCH_STORAGE_CAPABILITY){ return switches.cast(); } return super.getCapability(cap, side); } }   This actual thing is connected to a controller which allow you to register SwitchTile and I want to register a list of SwitchTile that is contained inside the Controller. This actually compile but doesn't work and each time I close the world it goes off  
    • Still not static: https://github.com/drmdgg/marijuanacraft1.14.4/blob/c4986b9a05326b9a17776e6000bf7e40957b3253/src/main/java/drmdgg/marijuanacraft/network/PacketButtonClicked.java#L27
    • ok I have done it and it works thank you for information
  • Topics

  • Who's Online (See full list)