Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Random drops from block


Razor
 Share

Recommended Posts

Heyyy, i try to make a block that drops the most of all the items in my mod (~100), but whats the easiest way to do this?

 

Thats my current way, but to much work for all my items..  Anyone an idea?:

 

    @Override
    public Item getItemDropped(IBlockState state, Random rand, int fortune) {
        switch(rand.nextInt(6)){
        case 1:
            return Item1;
        case 2:
            return Item2;
        case 3:
            return Item3;
        case 4:
            return Item4;
        case 5:
            return Item5;
        default:
            return Item6;
        }
    }

Link to comment
Share on other sites

You can create a public list in your item registry and add all of your items (at least those you want to be dropped from that item) ot that list while registering them. You can then select a random item from that list. I think it's propably the fastest way.

Link to comment
Share on other sites

11 hours ago, fcelon said:

You can create a public list in your item registry and add all of your items (at least those you want to be dropped from that item) ot that list while registering them. You can then select a random item from that list. I think it's propably the fastest way.

but how i make this without write all the items in

Link to comment
Share on other sites

1 hour ago, Razor said:

but how i make this without write all the items in

You can't. 

At some point, somewhere, you have to specify what you want dropped. 

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

1 hour ago, Razor said:

but how i make this without write all the items in

If you're lazy you can use this to obtain all Items added by your mod. But don't do this every time, do it once and store it in a field

 

List<Item> myItems = ForgeRegistries.ITEMS.getValues().stream()
  .filter(it -> it.getRegistryName().getResourceDomain().equals("yourmod"))
  .collect(Collectors.toList());
Edited by diesieben07
  • Like 1
Link to comment
Share on other sites

1 hour ago, diesieben07 said:

If you're lazy you can use this to obtain all Items added by your mod. But don't do this every time, do it once and store it in a field

 


List<Item> myItems = ForgeRegistries.ITEMS.getValues().stream()
  .filter(it -> it.getRegistryName().getResourceDomain().equals("yourmod"))
  .collect(Collectors.toList());

Yes, thats what i mean :D         And how can i get a single item for the drop out of this list ?   Sorry i´m new     ^^  

Link to comment
Share on other sites

1 hour ago, diesieben07 said:

Ahem.

 

Yes, i tried this:

 

     int index = rand.nextInt(myItems.size());
            Item item = myItems.get(index);
            return item;

 

but the console says:

 

java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException: bound must be positive

Link to comment
Share on other sites

  • 2 weeks later...
7 minutes ago, diesieben07 said:

I don't know why. You wrote the code and you have not posted it.

 

public class BlockBox extends Block {
    List<Item> myItems = ForgeRegistries.ITEMS.getValues().stream().filter(it -> it.getRegistryName().getResourceDomain().equals("ageofweapons")).collect(Collectors.toList());

 

    public BlockWeaponBox() {
        super(Material.WOOD);
        this.setCreativeTab(ModTabs.generalTab);
        this.setHardness(0.5F);
        this.setResistance(1.0F);
        this.setSoundType(SoundType.WOOD);       

    }

    
   
    
    @Override
    public Item getItemDropped(IBlockState state, Random rand, int fortune) {
         int index = rand.nextInt(myItems.size());
            Item item = myItems.get(index);
            return item;
        
    }
    
    
   

Link to comment
Share on other sites

You can't put it there, that's why. No items exist when that list is created.

  • Like 1

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • Capability class: public class ManaCapability { public static final ResourceLocation LOCATION=new ResourceLocation(ElderNorseGods.MOD_ID, "mana"); @CapabilityInject(IManaHandler.class) public static Capability<IManaHandler> CAP_INSTANCE=null; public static class ManaStorage implements IStorage<IManaHandler> { @Override public INBT writeNBT(Capability<IManaHandler> cap, IManaHandler manaHandler, Direction side) { CompoundNBT tag=new CompoundNBT(); tag.putFloat("Mana", manaHandler.getMana()); tag.putBoolean("Status", manaHandler.getStatus()); return tag; } @Override public void readNBT(Capability<IManaHandler> cap, IManaHandler manaHandler, Direction side, INBT nbt) { manaHandler.setMana(((CompoundNBT)nbt).getFloat("Mana")); manaHandler.setStatus(((CompoundNBT)nbt).getBoolean("Status")); } } public static class ManaProvider implements ICapabilitySerializable<CompoundNBT> { private final ManaHandler mana=new ManaHandler(); private final LazyOptional<IManaHandler> manaOptional=LazyOptional.of(()->this.mana); public void invalidate() { this.manaOptional.invalidate(); } @Override public CompoundNBT serializeNBT() { if(ManaCapability.CAP_INSTANCE==null) return new CompoundNBT(); else return (CompoundNBT)ManaCapability.CAP_INSTANCE.writeNBT(this.mana, null); } @Override public void deserializeNBT(CompoundNBT nbt) { if(ManaCapability.CAP_INSTANCE!=null) ManaCapability.CAP_INSTANCE.readNBT(this.mana, null, nbt); } @Override public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) { return cap==ManaCapability.CAP_INSTANCE?this.manaOptional.cast():LazyOptional.empty(); } } } Capability handler class: public class ManaHandler implements IManaHandler { private float mana; private boolean active; public ManaHandler() { this.mana=0F; this.active=false; } @Override public void setMana(float amount) { if(amount<0F) this.mana=0F; else if(amount>20F) this.mana=20F; else this.mana=amount; } @Override public float getMana() { return this.mana; } @Override public void setStatus(boolean value) { this.active=value; } @Override public boolean getStatus() { return this.active; } @Override public void update(ServerPlayerEntity player) { NetworkHandler.sendToPlayer(player, new ManaDataPacket(this.getMana(), this.getStatus())); } } Capability event handler class: @EventBusSubscriber(modid=ElderNorseGods.MOD_ID, bus=EventBusSubscriber.Bus.FORGE) public abstract class ManaEventHandler { private static void sendUpdates(ServerPlayerEntity player) { if(!player.getCommandSenderWorld().isClientSide()) { player.getCapability(ManaCapability.CAP_INSTANCE).ifPresent(mana->mana.update(player)); } } @SubscribeEvent public static void onPlayerChangedDimension(PlayerChangedDimensionEvent event) { ManaEventHandler.sendUpdates((ServerPlayerEntity)event.getPlayer()); } @SubscribeEvent public static void onPlayerRespawn(PlayerRespawnEvent event) { ManaEventHandler.sendUpdates((ServerPlayerEntity)event.getPlayer()); } @SubscribeEvent public static void onPlayerLoggedIn(PlayerLoggedInEvent event) { ManaEventHandler.sendUpdates((ServerPlayerEntity)event.getPlayer()); } @SubscribeEvent public static void onPlayerClone(PlayerEvent.Clone event) { if(!event.isWasDeath()) return; IManaHandler oldMana=event.getOriginal().getCapability(ManaCapability.CAP_INSTANCE).orElse(null); IManaHandler newMana=event.getPlayer().getCapability(ManaCapability.CAP_INSTANCE).orElse(null); if(oldMana!=null&&newMana!=null) { newMana.setMana(oldMana.getMana()); newMana.setStatus(oldMana.getStatus()); } } } Packet class: public class ManaDataPacket { private float value; private boolean status; public ManaDataPacket(float value, boolean status) { this.value=value; this.status=status; } public static void encode(ManaDataPacket packet, PacketBuffer buffer) { buffer.writeFloat(packet.value); buffer.writeBoolean(packet.status); } public static ManaDataPacket decode(PacketBuffer buffer) { return new ManaDataPacket(buffer.readFloat(), buffer.readBoolean()); } public static void handle(ManaDataPacket packet, Supplier<NetworkEvent.Context> ctx) { ctx.get().enqueueWork(()->DistExecutor.unsafeRunWhenOn(Dist.CLIENT, ()->()->PacketHandler.handlePacket(packet, ctx))); ctx.get().setPacketHandled(true); } private static class PacketHandler { private static void handlePacket(ManaDataPacket packet, Supplier<NetworkEvent.Context> ctx) { Minecraft mc=Minecraft.getInstance(); mc.player.getCapability(ManaCapability.CAP_INSTANCE).ifPresent ( mana-> { mana.setMana(packet.value); mana.setStatus(packet.status); } ); } } }
    • 1.12 is no longer supported on this forum. Please update to a modern version of Minecraft to receive support.
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.