Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix various NPE with the matter clusters #44

Merged
merged 3 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package fox.spiteful.avaritia;

import java.util.Random;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.util.ChatComponentTranslation;
Expand All @@ -10,19 +8,17 @@

public class DamageSourceInfinitySword extends EntityDamageSource {

private static final Random randy = new Random();

public DamageSourceInfinitySword(Entity source) {
super("infinity", source);
}

@Override
public IChatComponent func_151519_b(EntityLivingBase p_151519_1_) { // getDeathMessage
public IChatComponent func_151519_b(EntityLivingBase entity) { // getDeathMessage
String s = "death.attack.infinity";
int rando = randy.nextInt(5);
int rando = entity.worldObj.rand.nextInt(5);
if (rando != 0) s = s + "." + rando;
// func_145748_c_ = getFormattedCommandSenderName
return new ChatComponentTranslation(s, p_151519_1_.func_145748_c_(), this.damageSourceEntity.func_145748_c_());
return new ChatComponentTranslation(s, entity.func_145748_c_(), this.damageSourceEntity.func_145748_c_());
}

@Override
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/fox/spiteful/avaritia/LudicrousEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
Expand Down Expand Up @@ -39,7 +38,6 @@

public class LudicrousEvents {

private static Random randy = new Random();
static final String[] trash = new String[] { "dirt", "sand", "gravel", "cobblestone", "netherrack" };

@SubscribeEvent
Expand All @@ -66,7 +64,7 @@ public void onPlayerMine(PlayerInteractEvent event) {
event.entityPlayer);
} else {

if (block.quantityDropped(randy) == 0) {
if (block.quantityDropped(event.world.rand) == 0) {
ItemStack drop = block.getPickBlock(
ToolHelper.raytraceFromEntity(event.world, event.entityPlayer, true, 10),
event.world,
Expand Down Expand Up @@ -168,9 +166,9 @@ private static boolean isGarbage(ItemStack drop) {

public static void dropItem(ItemStack drop, World world, int x, int y, int z) {
float f = 0.7F;
double d0 = (double) (randy.nextFloat() * f) + (double) (1.0F - f) * 0.5D;
double d1 = (double) (randy.nextFloat() * f) + (double) (1.0F - f) * 0.5D;
double d2 = (double) (randy.nextFloat() * f) + (double) (1.0F - f) * 0.5D;
double d0 = (double) (world.rand.nextFloat() * f) + (double) (1.0F - f) * 0.5D;
double d1 = (double) (world.rand.nextFloat() * f) + (double) (1.0F - f) * 0.5D;
double d2 = (double) (world.rand.nextFloat() * f) + (double) (1.0F - f) * 0.5D;
EntityItem entityitem = new EntityItem(world, (double) x + d0, (double) y + d1, (double) z + d2, drop);
entityitem.delayBeforeCanPickup = 10;
world.spawnEntityInWorld(entityitem);
Expand Down
23 changes: 10 additions & 13 deletions src/main/java/fox/spiteful/avaritia/blocks/BlockCompressor.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package fox.spiteful.avaritia.blocks;

import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
Expand All @@ -25,7 +23,6 @@
public class BlockCompressor extends BlockContainer {

private IIcon top, sides, front;
private Random randy = new Random();

public BlockCompressor() {
super(Material.iron);
Expand Down Expand Up @@ -104,12 +101,12 @@ public void breakBlock(World world, int x, int y, int z, Block block, int wut) {
ItemStack itemstack = compressor.getStackInSlot(i);

if (itemstack != null) {
float f = this.randy.nextFloat() * 0.8F + 0.1F;
float f1 = this.randy.nextFloat() * 0.8F + 0.1F;
float f2 = this.randy.nextFloat() * 0.8F + 0.1F;
float f = world.rand.nextFloat() * 0.8F + 0.1F;
float f1 = world.rand.nextFloat() * 0.8F + 0.1F;
float f2 = world.rand.nextFloat() * 0.8F + 0.1F;

while (itemstack.stackSize > 0) {
int j1 = this.randy.nextInt(21) + 10;
int j1 = world.rand.nextInt(21) + 10;

if (j1 > itemstack.stackSize) {
j1 = itemstack.stackSize;
Expand All @@ -118,9 +115,9 @@ public void breakBlock(World world, int x, int y, int z, Block block, int wut) {
itemstack.stackSize -= j1;
EntityItem entityitem = new EntityItem(
world,
(double) ((float) x + f),
(double) ((float) y + f1),
(double) ((float) z + f2),
(float) x + f,
(float) y + f1,
(float) z + f2,
new ItemStack(itemstack.getItem(), j1, itemstack.getItemDamage()));

if (itemstack.hasTagCompound()) {
Expand All @@ -129,9 +126,9 @@ public void breakBlock(World world, int x, int y, int z, Block block, int wut) {
}

float f3 = 0.05F;
entityitem.motionX = (double) ((float) this.randy.nextGaussian() * f3);
entityitem.motionY = (double) ((float) this.randy.nextGaussian() * f3 + 0.2F);
entityitem.motionZ = (double) ((float) this.randy.nextGaussian() * f3);
entityitem.motionX = (float) world.rand.nextGaussian() * f3;
entityitem.motionY = (float) world.rand.nextGaussian() * f3 + 0.2F;
entityitem.motionZ = (float) world.rand.nextGaussian() * f3;
world.spawnEntityInWorld(entityitem);
}
}
Expand Down
23 changes: 10 additions & 13 deletions src/main/java/fox/spiteful/avaritia/blocks/BlockDireCrafting.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package fox.spiteful.avaritia.blocks;

import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
Expand All @@ -22,7 +20,6 @@
public class BlockDireCrafting extends BlockContainer {

private static IIcon top, sides, bottom;
private Random randy = new Random();

public BlockDireCrafting() {
super(Material.iron);
Expand Down Expand Up @@ -74,12 +71,12 @@ public void breakBlock(World world, int x, int y, int z, Block block, int wut) {
ItemStack itemstack = craft.getStackInSlot(i);

if (itemstack != null) {
float f = this.randy.nextFloat() * 0.8F + 0.1F;
float f1 = this.randy.nextFloat() * 0.8F + 0.1F;
float f2 = this.randy.nextFloat() * 0.8F + 0.1F;
float f = world.rand.nextFloat() * 0.8F + 0.1F;
float f1 = world.rand.nextFloat() * 0.8F + 0.1F;
float f2 = world.rand.nextFloat() * 0.8F + 0.1F;

while (itemstack.stackSize > 0) {
int j1 = this.randy.nextInt(21) + 10;
int j1 = world.rand.nextInt(21) + 10;

if (j1 > itemstack.stackSize) {
j1 = itemstack.stackSize;
Expand All @@ -88,9 +85,9 @@ public void breakBlock(World world, int x, int y, int z, Block block, int wut) {
itemstack.stackSize -= j1;
EntityItem entityitem = new EntityItem(
world,
(double) ((float) x + f),
(double) ((float) y + f1),
(double) ((float) z + f2),
(float) x + f,
(float) y + f1,
(float) z + f2,
new ItemStack(itemstack.getItem(), j1, itemstack.getItemDamage()));

if (itemstack.hasTagCompound()) {
Expand All @@ -99,9 +96,9 @@ public void breakBlock(World world, int x, int y, int z, Block block, int wut) {
}

float f3 = 0.05F;
entityitem.motionX = (double) ((float) this.randy.nextGaussian() * f3);
entityitem.motionY = (double) ((float) this.randy.nextGaussian() * f3 + 0.2F);
entityitem.motionZ = (double) ((float) this.randy.nextGaussian() * f3);
entityitem.motionX = (float) world.rand.nextGaussian() * f3;
entityitem.motionY = (float) world.rand.nextGaussian() * f3 + 0.2F;
entityitem.motionZ = (float) world.rand.nextGaussian() * f3;
world.spawnEntityInWorld(entityitem);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package fox.spiteful.avaritia.blocks;

import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
Expand All @@ -25,7 +23,6 @@
public class BlockNeutronCollector extends BlockContainer {

private IIcon top, sides, front;
private Random randy = new Random();

public BlockNeutronCollector() {
super(Material.iron);
Expand Down Expand Up @@ -103,12 +100,12 @@ public void breakBlock(World world, int x, int y, int z, Block block, int wut) {
ItemStack itemstack = collector.getStackInSlot(0);

if (itemstack != null) {
float f = this.randy.nextFloat() * 0.8F + 0.1F;
float f1 = this.randy.nextFloat() * 0.8F + 0.1F;
float f2 = this.randy.nextFloat() * 0.8F + 0.1F;
float f = world.rand.nextFloat() * 0.8F + 0.1F;
float f1 = world.rand.nextFloat() * 0.8F + 0.1F;
float f2 = world.rand.nextFloat() * 0.8F + 0.1F;

while (itemstack.stackSize > 0) {
int j1 = this.randy.nextInt(21) + 10;
int j1 = world.rand.nextInt(21) + 10;

if (j1 > itemstack.stackSize) {
j1 = itemstack.stackSize;
Expand All @@ -117,19 +114,19 @@ public void breakBlock(World world, int x, int y, int z, Block block, int wut) {
itemstack.stackSize -= j1;
EntityItem entityitem = new EntityItem(
world,
(double) ((float) x + f),
(double) ((float) y + f1),
(double) ((float) z + f2),
(float) x + f,
(float) y + f1,
(float) z + f2,
new ItemStack(itemstack.getItem(), j1, itemstack.getItemDamage()));

if (itemstack.hasTagCompound()) {
entityitem.getEntityItem().setTagCompound((NBTTagCompound) itemstack.getTagCompound().copy());
}

float f3 = 0.05F;
entityitem.motionX = (double) ((float) this.randy.nextGaussian() * f3);
entityitem.motionY = (double) ((float) this.randy.nextGaussian() * f3 + 0.2F);
entityitem.motionZ = (double) ((float) this.randy.nextGaussian() * f3);
entityitem.motionX = (float) world.rand.nextGaussian() * f3;
entityitem.motionY = (float) world.rand.nextGaussian() * f3 + 0.2F;
entityitem.motionZ = (float) world.rand.nextGaussian() * f3;
world.spawnEntityInWorld(entityitem);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package fox.spiteful.avaritia.compat.ticon;

import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.Entity;
Expand All @@ -24,8 +22,6 @@

public class TonkersEvents {

private Random randy = new Random();

@SubscribeEvent
public void craftTool(ToolCraftEvent.NormalTool event) {
NBTTagCompound toolTag = event.toolTag.getCompoundTag("InfiTool");
Expand Down Expand Up @@ -142,7 +138,7 @@ public void onPlayerMine(PlayerInteractEvent event) {

if (toolTag != null && toolTag.getInteger("Head") == Tonkers.infinityMetalId
&& tool.canHarvestBlock(Blocks.stone, held)) {
if (block.quantityDropped(randy) == 0) {
if (block.quantityDropped(event.world.rand) == 0) {
ItemStack drop = block.getPickBlock(
ToolHelper.raytraceFromEntity(event.world, event.entityPlayer, true, 10),
event.world,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fox.spiteful.avaritia.entity;

import java.util.List;
import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.command.IEntitySelector;
Expand All @@ -16,7 +15,6 @@

public class EntityGapingVoid extends Entity {

private static Random randy = new Random();
public static final int maxLifetime = 186;
public static double collapse = .95;
public static double suckrange = 20.0;
Expand Down Expand Up @@ -96,8 +94,8 @@ public void onUpdate() {
double size = getVoidScale(age) * 0.5 - 0.2;
for (int i = 0; i < 50; i++) {
Vec3 pootdir = Vec3.createVectorHelper(0, 0, size);
pootdir.rotateAroundY(randy.nextFloat() * 180f);
pootdir.rotateAroundX(randy.nextFloat() * 360f);
pootdir.rotateAroundY(rand.nextFloat() * 180f);
pootdir.rotateAroundX(rand.nextFloat() * 360f);

Vec3 pootspeed = pootdir.normalize();
pootspeed.xCoord *= particlespeed;
Expand Down
42 changes: 23 additions & 19 deletions src/main/java/fox/spiteful/avaritia/items/ItemMatterCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;

import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.renderer.texture.IIconRegister;
Expand All @@ -29,8 +28,6 @@

public class ItemMatterCluster extends Item implements ICosmicRenderItem {

protected static Random randy = new Random();

public static final String MAINTAG = "clusteritems";
public static final String LISTTAG = "items";
public static final String ITEMTAG = "item";
Expand Down Expand Up @@ -85,12 +82,15 @@ public void addInformation(ItemStack stack, EntityPlayer player, List<String> to
NBTTagCompound tag = list.getCompoundTagAt(i);
ItemStack countstack = ItemStack.loadItemStackFromNBT(tag.getCompoundTag(ITEMTAG));
int count = tag.getInteger(COUNTTAG);

tooltip.add(
countstack.getItem().getRarity(countstack).rarityColor + countstack.getDisplayName()
+ EnumChatFormatting.GRAY
+ " x "
+ count);
if (countstack != null) {
tooltip.add(
countstack.getItem().getRarity(countstack).rarityColor + countstack.getDisplayName()
+ EnumChatFormatting.GRAY
+ " x "
+ count);
} else {
tooltip.add(EnumChatFormatting.RED + "DELETED" + EnumChatFormatting.GRAY + " x " + count);
}
}
} else {
tooltip.add(EnumChatFormatting.DARK_GRAY + StatCollector.translateToLocal("tooltip.matter_cluster.desc"));
Expand All @@ -102,12 +102,11 @@ public void addInformation(ItemStack stack, EntityPlayer player, List<String> to

public static List<ItemStack> makeClusters(List<ItemStack> input) {
Map<ItemStackWrapper, Integer> items = ToolHelper.collateMatterCluster(input);
List<ItemStack> clusters = new ArrayList<ItemStack>();
List<Entry<ItemStackWrapper, Integer>> itemlist = new ArrayList<Entry<ItemStackWrapper, Integer>>();
itemlist.addAll(items.entrySet());
List<ItemStack> clusters = new ArrayList<>();
List<Entry<ItemStackWrapper, Integer>> itemlist = new ArrayList<>(items.entrySet());

int currentTotal = 0;
Map<ItemStackWrapper, Integer> currentItems = new HashMap<ItemStackWrapper, Integer>();
Map<ItemStackWrapper, Integer> currentItems = new HashMap<>();

while (!itemlist.isEmpty()) {
Entry<ItemStackWrapper, Integer> e = itemlist.get(0);
Expand All @@ -134,7 +133,7 @@ public static List<ItemStack> makeClusters(List<ItemStack> input) {
clusters.add(cluster);

currentTotal = 0;
currentItems = new HashMap<ItemStackWrapper, Integer>();
currentItems = new HashMap<>();
}
}

Expand All @@ -159,15 +158,21 @@ public static ItemStack makeCluster(Map<ItemStackWrapper, Integer> input) {

public static Map<ItemStackWrapper, Integer> getClusterData(ItemStack cluster) {
if (!cluster.hasTagCompound() || !cluster.getTagCompound().hasKey(MAINTAG)) {
return null;
return new HashMap<>();
}
NBTTagCompound tag = cluster.getTagCompound().getCompoundTag(MAINTAG);
NBTTagList list = tag.getTagList(LISTTAG, 10);
Map<ItemStackWrapper, Integer> data = new HashMap<ItemStackWrapper, Integer>();
Map<ItemStackWrapper, Integer> data = new HashMap<>();

for (int i = 0; i < list.tagCount(); i++) {
NBTTagCompound entry = list.getCompoundTagAt(i);
ItemStackWrapper wrap = new ItemStackWrapper(ItemStack.loadItemStackFromNBT(entry.getCompoundTag(ITEMTAG)));
final ItemStack stack = ItemStack.loadItemStackFromNBT(entry.getCompoundTag(ITEMTAG));
if (stack == null) {
// item might be null if the cluster contains items that don't exist
// anymore by removing a mod for example
continue;
}
ItemStackWrapper wrap = new ItemStackWrapper(stack);
int count = entry.getInteger(COUNTTAG);
data.put(wrap, count);
}
Expand Down Expand Up @@ -211,8 +216,7 @@ public static void mergeClusters(ItemStack donor, ItemStack recipient) {

Map<ItemStackWrapper, Integer> donordata = getClusterData(donor);
Map<ItemStackWrapper, Integer> recipientdata = getClusterData(recipient);
List<Entry<ItemStackWrapper, Integer>> datalist = new ArrayList<Entry<ItemStackWrapper, Integer>>();
datalist.addAll(donordata.entrySet());
List<Entry<ItemStackWrapper, Integer>> datalist = new ArrayList<>(donordata.entrySet());

while (recipientcount < capacity && donorcount > 0) {
Entry<ItemStackWrapper, Integer> e = datalist.get(0);
Expand Down
Loading