Skip to content

Commit 45befb9

Browse files
davenonymousthraaawn
authored andcommitted
Start players with a shrinking device in their inventory
1 parent 8a37187 commit 45befb9

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

src/main/java/org/dave/compactmachines3/skyworld/SkyWorldEvents.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package org.dave.compactmachines3.skyworld;
22

3+
import net.minecraft.entity.item.EntityItem;
4+
import net.minecraft.item.ItemStack;
35
import net.minecraft.util.math.BlockPos;
46
import net.minecraft.util.text.TextComponentTranslation;
57
import net.minecraft.world.World;
68
import net.minecraft.world.WorldServer;
79
import net.minecraftforge.event.world.BlockEvent;
810
import net.minecraftforge.event.world.WorldEvent;
911
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
12+
import net.minecraftforge.fml.common.gameevent.PlayerEvent;
1013
import net.minecraftforge.fml.common.gameevent.TickEvent;
14+
import org.dave.compactmachines3.init.Itemss;
1115
import org.dave.compactmachines3.utility.Logz;
16+
import org.dave.compactmachines3.utility.ShrinkingDeviceUtils;
1217

1318
public class SkyWorldEvents {
1419
@SubscribeEvent
@@ -18,7 +23,7 @@ public static void onPlayerTick(TickEvent.PlayerTickEvent event) {
1823
return;
1924
}
2025

21-
if(event.player.isCreative() || event.player.isSpectator()) {
26+
if(event.player.isSpectator()) {
2227
return;
2328
}
2429

@@ -27,13 +32,30 @@ public static void onPlayerTick(TickEvent.PlayerTickEvent event) {
2732
return;
2833
}
2934

30-
if(event.player.posY > 49.0f || event.player.posY < 39.5f) {
35+
if(!event.player.isCreative() && (event.player.posY > 49.0f || event.player.posY < 39.5f)) {
3136
BlockPos spawnPoint = worldServer.getSpawnPoint();
3237
event.player.setPositionAndUpdate(spawnPoint.getX() + 0.5d, spawnPoint.getY() + 0.2d, spawnPoint.getZ() + 0.5d);
3338
return;
3439
}
40+
41+
if(!ShrinkingDeviceUtils.hasShrinkingDeviceInInventory(event.player) && !SkyWorldSavedData.instance.hasReceivedStartingInventory(event.player)) {
42+
ItemStack psdStack = new ItemStack(Itemss.psd, 1, 0);
43+
if(!event.player.addItemStackToInventory(psdStack)) {
44+
EntityItem entityItem = new EntityItem(world, event.player.posX, event.player.posY + event.player.getEyeHeight() + 0.5f, event.player.posZ, psdStack);
45+
entityItem.lifespan = 2400;
46+
entityItem.setPickupDelay(10);
47+
48+
entityItem.motionX = 0.0f;
49+
entityItem.motionY = 0.15f;
50+
entityItem.motionZ = 0.0f;
51+
world.spawnEntity(entityItem);
52+
}
53+
54+
SkyWorldSavedData.instance.addToStartingInventoryReceiverSet(event.player);
55+
}
3556
}
3657

58+
3759
@SubscribeEvent
3860
public static void createSpawnPoint(WorldEvent.CreateSpawnPosition event) {
3961
World world = event.getWorld();

src/main/java/org/dave/compactmachines3/skyworld/SkyWorldSavedData.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ public class SkyWorldSavedData extends WorldSavedData {
2121
public static SkyWorldSavedData instance;
2222

2323
private Set<UUID> hubMachineOwners;
24+
private Set<UUID> startingInventoryReceivers;
2425

2526
public SkyWorldSavedData(String name) {
2627
super(name);
2728

2829
hubMachineOwners = new HashSet<>();
30+
startingInventoryReceivers = new HashSet<>();
2931
}
3032

3133
public boolean isHubMachineOwner(EntityPlayer player) {
@@ -37,6 +39,15 @@ public void addToHubMachineOwners(EntityPlayer player) {
3739
this.markDirty();
3840
}
3941

42+
public boolean hasReceivedStartingInventory(EntityPlayer player) {
43+
return startingInventoryReceivers.contains(player.getUniqueID());
44+
}
45+
46+
public void addToStartingInventoryReceiverSet(EntityPlayer player) {
47+
startingInventoryReceivers.add(player.getUniqueID());
48+
this.markDirty();
49+
}
50+
4051
@SubscribeEvent
4152
public static void loadWorld(WorldEvent.Load event) {
4253
World world = event.getWorld();
@@ -68,6 +79,17 @@ public void readFromNBT(NBTTagCompound nbt) {
6879
hubMachineOwners.add(uuid);
6980
}
7081
}
82+
83+
startingInventoryReceivers.clear();
84+
if(nbt.hasKey("startingInventoryReceivers")) {
85+
NBTTagList tagList = nbt.getTagList("startingInventoryReceivers", Constants.NBT.TAG_COMPOUND);
86+
for(NBTBase baseUUID : tagList) {
87+
NBTTagCompound compoundUUID = (NBTTagCompound)baseUUID;
88+
UUID uuid = compoundUUID.getUniqueId("");
89+
90+
startingInventoryReceivers.add(uuid);
91+
}
92+
}
7193
}
7294

7395
@Override
@@ -81,6 +103,15 @@ public NBTTagCompound writeToNBT(NBTTagCompound compound) {
81103

82104
compound.setTag("hubMachineOwners", hubMachineOwnersTagList);
83105

106+
NBTTagList startingInventoryReceiversTagList = new NBTTagList();
107+
for(UUID uuid : startingInventoryReceivers) {
108+
NBTTagCompound compoundUUID = new NBTTagCompound();
109+
compoundUUID.setUniqueId("", uuid);
110+
startingInventoryReceiversTagList.appendTag(compoundUUID);
111+
}
112+
113+
compound.setTag("startingInventoryReceivers", startingInventoryReceiversTagList);
114+
84115
return compound;
85116
}
86117
}

0 commit comments

Comments
 (0)