-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModControlBlocks.java
More file actions
212 lines (174 loc) · 7.23 KB
/
ModControlBlocks.java
File metadata and controls
212 lines (174 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package com.lothrazar.samscontrolblocks;
import java.util.ArrayList;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Logger;
import com.lothrazar.samscontrolblocks.proxy.*;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvent;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.Mod.Instance;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.InputEvent;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import net.minecraftforge.fml.relauncher.Side;
@Mod(modid = ModControlBlocks.MODID, useMetadata=true, updateJSON="https://raw.githubusercontent.com/LothrazarMinecraftMods/BlockControl/master/update.json")
public class ModControlBlocks
{
public static final String MODID = "samscontrolblocks";
public static final String TEXTURE_LOCATION = MODID + ":";
@Instance(value = MODID)
public static ModControlBlocks instance;
@SidedProxy(clientSide="com.lothrazar.samscontrolblocks.proxy.ClientProxy", serverSide="com.lothrazar.samscontrolblocks.proxy.CommonProxy")
public static CommonProxy proxy;
public static Logger logger;
public static SimpleNetworkWrapper network;
@EventHandler
public void onPreInit(FMLPreInitializationEvent event)
{
logger = event.getModLog();
Configuration config = new Configuration(event.getSuggestedConfigurationFile());
config.load();
String csv = config.getString("push_pull_ignore",MODID, "minecraft:cactus,minecraft:bedrock",
"Ignore list for push and pull keys.");
UtilMoveBlock.seIgnoreBlocksFromString(csv);
if(config.hasChanged()){config.save();}
network = NetworkRegistry.INSTANCE.newSimpleChannel( MODID );
network.registerMessage(MessageKeyPush.class, MessageKeyPush.class, MessageKeyPush.ID, Side.SERVER);
network.registerMessage(MessageKeyPull.class, MessageKeyPull.class, MessageKeyPull.ID, Side.SERVER);
network.registerMessage(MessageKeyRotate.class, MessageKeyRotate.class, MessageKeyRotate.ID, Side.SERVER);
this.registerEventHandlers();
}
public static String posToCSV(BlockPos pos)
{
return pos.getX()+","+pos.getY()+","+pos.getZ();
}
public static BlockPos stringCSVToBlockPos(String csv)
{
String [] spl = csv.split(",");
return new BlockPos(Integer.parseInt(spl[0]),Integer.parseInt(spl[1]),Integer.parseInt(spl[2]));
}
@EventHandler
public void onInit(FMLInitializationEvent event)
{
proxy.registerRenderers();
}
private void registerEventHandlers()
{
MinecraftForge.EVENT_BUS.register(instance);
}
public static void incrementPlayerIntegerNBT(EntityPlayer player, String prop, int inc)
{
int prev = player.getEntityData().getInteger(prop);
prev += inc;
player.getEntityData().setInteger(prop, prev);
}
@SubscribeEvent
public void onKeyInput(InputEvent.KeyInputEvent event)
{
if(Minecraft.getMinecraft().objectMouseOver == null){return;}
BlockPos posMouse = Minecraft.getMinecraft().objectMouseOver.getBlockPos();
if(posMouse == null){return;}
//send different packet depending on which key is pressed
if(ClientProxy.keyPush.isPressed())
{
ModControlBlocks.network.sendToServer( new MessageKeyPush(posMouse));
}
else if(ClientProxy.keyPull.isPressed())
{
ModControlBlocks.network.sendToServer( new MessageKeyPull(posMouse));
}
else if(ClientProxy.keyTransform.isPressed())
{
ModControlBlocks.network.sendToServer( new MessageKeyRotate(posMouse));
}
}
public static void playSoundAt(World world,BlockPos pos, String sound)
{
world.playSound((double)pos.getX(), (double)pos.getY(), (double)pos.getZ(), new SoundEvent(new ResourceLocation(sound)), SoundCategory.MASTER,1.0F, 1.0F,false);
}
public static ArrayList<Block> getBlockListFromCSV(String csv)
{
ArrayList<Block> blocks = new ArrayList<Block>();
String[] ids = csv.split(",");
Block b;
for(String id : ids)
{
b = Block.getBlockFromName(id);
if(b == null)
{
ModControlBlocks.logger.log(Level.WARN, "getBlockListFromCSV : Block not found : "+id);
}
else
{
blocks.add(b);
}
}
return blocks;
}
public static void playSoundAt(Entity player, String sound)
{
player.world.playSound((double)player.getPosition().getX(), (double)player.getPosition().getY(), (double)player.getPosition().getZ(), new SoundEvent(new ResourceLocation(sound)), SoundCategory.MASTER,1.0F, 1.0F,false);
}
public static String lang(String name)
{
return name;
}
public static void addChatMessage(String string)
{
addChatMessage(string);
}
public static void addChatMessage(ITextComponent string)
{
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(string);
}
public static void addChatMessage(EntityPlayer player,String string)
{
player.sendMessage(new TextComponentString(string));
}
public static EnumFacing getPlayerFacing(EntityPlayer player)
{
int yaw = (int)player.rotationYaw;
if (yaw<0) //due to the yaw running a -360 to positive 360
yaw+=360; //not sure why it's that way
yaw += 22; //centers coordinates you may want to drop this line
yaw %= 360; //and this one if you want a strict interpretation of the zones
int facing = yaw/45; // 360degrees divided by 45 == 8 zones
return EnumFacing.getHorizontal( facing/2 );
}
public static void spawnParticle(World world, EnumParticleTypes type, BlockPos pos)
{
spawnParticle(world,type,pos.getX(),pos.getY(),pos.getZ());
}
public static void spawnParticle(World world, EnumParticleTypes type, double x, double y, double z)
{
//http://www.minecraftforge.net/forum/index.php?topic=9744.0
for(int countparticles = 0; countparticles <= 10; ++countparticles)
{
world.spawnParticle(type, x + (world.rand.nextDouble() - 0.5D) * (double)0.8, y + world.rand.nextDouble() * (double)1.5 - (double)0.1, z + (world.rand.nextDouble() - 0.5D) * (double)0.8, 0.0D, 0.0D, 0.0D);
}
}
public static void execute(EntityPlayer player, String cmd)
{
FMLCommonHandler.instance().getMinecraftServerInstance().getCommandManager().executeCommand(player, cmd);
}
}